This question already has answers here:
Parsing a date’s ordinal indicator ( st, nd, rd, th ) in a date-time string
(5 answers)
Closed 4 years ago.
I have a date of the form "20th Oct 2017" how do I convert it to "2017-10-20". In specific I don't see the format for 20th. I am able to get the format for month and year "MMM yyyy" I would like to know the format for the day.
Try this one:
DateTimeFormatter parseFormatter = DateTimeFormatter.ofPattern("MMM d['st']['nd']['rd']['th'] uuuu h:mma", Locale.ENGLISH);
LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, parseFormatter);
Considering Java 8 features, you can format your date in various ways.
Just check the following formats and ways of parsing and formatting the date:
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
Related
This question already has answers here:
Convert Java Date to UTC String
(7 answers)
How do you format current system datetime as UTC using String.format in Java?
(2 answers)
How to get UTC+0 date in Java 8?
(5 answers)
Closed last month.
This post was edited and submitted for review last month and failed to reopen the post:
Original close reason(s) were not resolved
I am trying to print a date in this format:
2023-01-11 09:25:52 UTC
But when I use date format:
yyyy-MM-dd HH:mm:ss Z
I get:
2023-01-11 09:29:25 +0100
While searching by existing question in stack overflow, I found similar questions but not with this exact format with "UTC" at the end. I found one that provided a solution to add the format as yyyy-MM-dd HH:mm:ss 'UTC' but then it would force the a GMT value to wrongly show UTC at the end.
Most of the answers explain how to get UTC value, but not how to print in this format
2023-01-11 09:25:52 UTC
Some solutions were also suggesting to use something else than SimpleDateFormat.
This question was marked as duplicated, but none of the post that were supposed to be duplicated had the info that I wanted.
Use a lowercase z instead of Z to get the offset instead of the id. And you have to set the time zone using simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")).
Example:
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(simpleDateFormat.format(date)); // 2023-01-11 08:41:17 UTC
If you create an instance of simpleDateFormat with yyyy-MM-dd HH:mm:ss Z date format is incorrect change to capital Z to small z,
then it works as expected.
2023-01-11 09:25:52 UTC
This question already has answers here:
How to convert 24 hr format time in to 12 hr Format?
(16 answers)
Closed 2 years ago.
I have a time in the format HHMM (1643) and I want it to be formatted to HH:MM AM/PM (04:43 PM)
How can I achieve this in Java?
I'm doing something similar for the date which is formatted YYYMMDD (20201013) into MM/DD/YYY (10/13/2020) with this code:
new java.text.SimpleDateFormat("yyyyMMdd").parse($F{date})
You can use DateTimeFormatter & LocalTime from java.time
First, parse the time as LocalTime using DateTimeFormatter. Then again format the LocalTime into String in your desire format.
LocalTime time = LocalTime.parse("1643", DateTimeFormatter.ofPattern("HHmm"));
String formattedTime = time.format(DateTimeFormatter.ofPattern("hh:mm a"));
System.err.println(formattedTime);
Output: 04:43 PM
This question already has answers here:
"EEE MMM dd HH:mm:ss ZZZ yyyy" date format to java.sql.Date
(2 answers)
Closed 5 years ago.
I'm working on an android application , when i need to send my current date to the server , i get my actual date by currentTime = Calendar.getInstance().getTime(); and i'm parsing it using SimpleDateFormat "yyyy/MM/DD" , but the value i got for day is very weird : 38
There is the code i'm using :
currentTime = Calendar.getInstance().getTime();
SimpleDateFormat df1 = new SimpleDateFormat("yyyy/MM/DD");
String formattedDate1 = df1.format(currentTime.getTime());
NB : The date value returned by currentTime is correct
PS : This code was working correctly last days !
The pattern matching for parsing dates is case sensitive. You need to be very careful when generating your parsing pattern and refer to the documentation.
DD, all capital/uppercase letters, refers to the current day in the year. Today, 7 February, is the 38th day in the year (31 in January + 7 in February.)
dd, all lowercase letters, refers to the current day in the month. This would parse as 7 for 7 February.
The correct pattern you should be using, therefore, is: yyyy/MM/dd.
It was working recently, because it was previously January. For the month of January only, dd and DD will return the same value.
I believe you want d not DD
D returns the current day in year (31 days in Jan + 7 in Feb, I'm assuming).
d returns current day in month.
sauce: https://developer.android.com/reference/java/text/SimpleDateFormat.html
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 5 years ago.
I try to use java.util.Date date = Date.from( Instant.parse(minDates)); to parse the date string given in format Wed Jan 17 2001 00:00:00 GMT 0530.
I am not able to figure out, how to do that in JAVA.
The want to convert the given date string in given format
2013-05-22T00:00:00
May be i am not able to figure it out, properly. If someone have way to do that suggest me in Java Only.
Here is the solution:
String dateToParse = "Wed Jan 17 2001 00:00:00 GMT 0530";
SimpleDateFormat in = new SimpleDateFormat("EEE MMM dd YYYY HH:mm:ss");
SimpleDateFormat out = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm:ss");
Date date = in.parse( dateToParse );
System.out.println( out.format( date ) );
It will work if all dates are in the same timezone (GMT 0530)
Else it should be modified to support it, but I suppose you have the same timezone.
You can do that by using SimpleDateFormat 'parse' API.
You can initialize your SimpleDateFormat with any valid time format such as yyyy-MM-dd HH:mm:ss z and then parse any string which adheres to this format.
reff. to http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
One addition tip, use JodaTime as the Date and SDF in Java are getting deprecated: http://www.joda.org/joda-time/
If you are using Java 8+, You can use java.time.OffsetDateTime (or Instant...) instead of java.util.Date, which is incredibly easy.
OffsetDateTime odt = OffsetDateTime .parse("2013-05-22T00:00:00", DateTimeFormatter.ISO_LOCAL_DATE_TIME);
Note that the second argument is optional in this case but you could have to specify one (with timezone id for example).
There is a solution without external which works with older version of Java and that manages timezones well. It consists of using JAXB's DataTypeConverter.
Date date = javax.xml.bind.DatatypeConverter.parseDateTime("2013-05-22T00:00:00+01:00").getTime();
Note that DatatypeConverter.parseDateTime returns a Calendar. You just need to call its getTime() method to convert is to a Date.
This question already has answers here:
SimpleDateFormat producing wrong date time when parsing "YYYY-MM-dd HH:mm"
(5 answers)
Closed 6 years ago.
The code I've got should convert the date of birth input to DD/MM/YYYY format, which it does but for example when I input 20/08/2000 it sees the date as 3rd January.
System.out.println(this.dob);
DateFormat dateF = new SimpleDateFormat("dd/MM/YYYY");
Date birth = dateF.parse(this.dob);
System.out.println(birth);
Which outputs
20/08/2000
Mon Jan 03 00:00:00 GMT 2000
Using capital Ys in your format means something called the "week year".
Instead, use lowercase ys in your format, which means the year as you'd expect.
new SimpleDateFormat("dd/MM/yyyy");