This question already has answers here:
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Closed 8 years ago.
I'm processing a list of dates from an input file, and I need to convert each from String to Date. Examples of the format:
9/2/2013 7:34:17 PM
1/13/2011 10:47:36 AM
Each time a line is read, the date is stored in the String variable dateAsString. Here's what I've got:
DateFormat format = new SimpleDateFormat("MM/dd/YYYY hh:mm:ss a");
Date myDate = format.parse(dateAsString);
System.out.println(myDate.toString());
The output is incorrect:
9/2/2013 7:34:17 PM becomes Sun Dec 30 19:34:17 EST 2012
1/13/2011 10:47:36 AM becomes Sun Dec 26 10:47:36 EST 2010
It seems pretty straightforward, so I'm confused. What am I doing wrong?
Just try 'yyyy' instead of 'YYYY'
Capital 'Y' means a "week year" where 'y' represents the actual year. Try using yyyy instead.
For more formatting help, check out the SimpleDateFormat API where they give you examples of the patterns to use.
Extra Info
Just in case you're wondering, a "week year" is a year where all the weeks in the year are whole weeks
Your format is not correct, as you can see in Javadoc :
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Use yyyy instead of YYYY for the year part
Related
This question already has answers here:
ParseException when parsing 3 character abbreviated month using SimpleDateFormat
(5 answers)
Closed 4 years ago.
i have a SimpleDateFormat format = new SimpleDateFormat("d M y H:m"); and i try to parse the String "8 Jan 2019 16:47" with it, but i get a ParseException. Did i create it the wrong way?
According to docs.oracle.com the M should recognize 3-letter-months.
Can anyone help me?
The official documentation: (https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html)
You probably missed out this little note here:
Month: If the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number.
Based on your example input, the following works:
SimpleDateFormat format = new SimpleDateFormat("dd MMM yyyy HH:mm");
java.time
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d MMM y H:mm", Locale.ENGLISH);
String stringToParse = "8 Jan 2019 16:47";
LocalDateTime dateTime = LocalDateTime.parse(stringToParse, formatter);
System.out.println(dateTime);
The output from this snippet is:
2019-01-08T16:47
What went wrong in your code?
SimpleDateFormat and Date are poorly designed and long outdated, the former in particular notoriously troublesome. I recommend you don’t use them in 2019.
As others have said you need three M for month abbreviation (no matter if you are using the outdated SimpleDateFormat or the modern DateTimeFormatter). One M will match a month number in 1 or 2 digits, for example 1 for January.
You should also specify a locale for your formatter. I took Jan to be English so specified Locale.ENGLISH. If you don’t specify locale, the JVM’s default locale will be used, which may work well on some JVMs and suddenly break some day when the default locale has been changed or you are trying to run your program on a different computer.
Link
Oracle tutorial: Date Time explaining how to use java.time.
This question already has answers here:
Unix epoch time to Java Date object
(7 answers)
convert millisecond to Joda Date Time or for zone 0000
(3 answers)
Closed 4 years ago.
I have Long value 1282680754000 where if I check this value in https://www.epochconverter.com/ it gives me Tuesday, August 24, 2010 8:12:34 PM
But if I use new DateTime(1282680754000).toDate() I get Wed Aug 25 01:42:34 IST 2010 (It is adding +5.30 hour)
How to get Tuesday, August 24, 2010 8:12:34 PM for 1282680754000 in java
Just use
Instant.ofEpochMilli(1_282_680_754_000L)
or
Instant.ofEpochMilli(1_282_680_754_000L).atOffset(ZoneOffset.UTC)
(using java.time, the modern Java date and time API; you may consider it the successor of Joda-Time).
The latter will give you an OffsetDateTime, which you can then format into youe desired format.
What went wrong in your code?
Your code is correct. You got the correct Date. The only things are:
For most purposes you shouldn’t want a java.util.Date. That class is long outdated and has design problems, which was the major background for development of Joda-Time and later java.time.
Your Date was printed in your local time (IST, probably India Standard Time or Asia/Kolkata) where you expected UTC. A Date has got neither time zone nor offset in it. When you print it, its toString method grabs your JVM’s time zone setting and renders the time in this time zone — in your case in IST. This behaviour surprises many.
Link: All about java.util.Date on Jon Skeet’s coding blog
String result = DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy h:mm:ss a")
.withZone(ZoneId.of("UTC"))
.toFormat()
.format(Instant.ofEpochMilli(1282680754000L));
System.out.println(result); // Tuesday, August 24, 2010 8:12:34 PM
Omit the toDate, you are converting it to a Date object, which its toString method uses your operating system default time zone to print its value (you can see the IST in your question).
Just for reference examine the follows:
public static void main(String[] args) {
DateTime dateTime = new DateTime(1282680754000L, DateTimeZone.forID("GMT"));
System.out.println(dateTime.toDate().toGMTString());
}
24 Aug 2010 20:12:34 GMT
against (my default time zone is IDT):
public static void main(String[] args) {
DateTime dateTime = new DateTime(1282680754000L,DateTimeZone.forID("GMT"));
System.out.println(dateTime.toDate());
}
Tue Aug 24 23:12:34 IDT 2010
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");
This question already has answers here:
java date problem in parsing
(4 answers)
Closed 7 years ago.
I am getting a date by ajax in String format. But it is getting changed when I am converting it to date by SimpleDateFormat. The month is always changed to Jan. I am worried only about the month change.My code is given below
String appointmentDate = request.getParameter("appointmentDate");
System.out.println(" appointment date in String format "+appointmentDate);
Here I am getting the date correctly(16/12/2015). But when I am changing it to Date format it is getting changed(Fri Jan 16 00:12:00 IST 2015). Whatever I input the month, say August, May, June, I am always getting month Jan.
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy");
Date parsedDate = dateFormat.parse(appointmentDate);
System.out.println(" appointment date in DATE format "+parsedDate);
Please help me out. Thanks in advance.
As per the JavaDoc, lower case m denotes minutes, not months.
Changing your expression to dd/MM/yyyy should fix the issue.
This question already has answers here:
How to convert a date String to a Date or Calendar object?
(5 answers)
Closed 6 years ago.
As I am not expert in handling of dates in java but I am unable to understand this behaviour.Here is my code
Date from = new SimpleDateFormat("dd/MM/yyyy").parse("05/07/2013");
System.out.println(from);
which gave me this output
Sat Jul 05 00:07:00 PKT 2013
And this is 2nd another code snippet
Date from = new SimpleDateFormat("dd/mm/yyyy").parse("05/07/2013");
System.out.println(from);
which gave me this output:
Sat Jan 05 00:07:00 PKT 2013
Now the thing which is considerable is format. This format dd/MM/yyyy which have MM gave me correct output but this format dd/mm/yyyy which have small mm gave me wrong output (always give jan in month).I read the doc where it is mentioned that samll m is for minutes and capital M is for month My question is Can I never use small m here? if no , then why it is giving the result and on which basis it is giving jan everytime I know this is a basic question but after searching and after not finding any understandable thing , I posted it.Thanks
Date from = new SimpleDateFormat("dd/mm/yyyy").parse("05/07/2013");
that mm in your format is for minutes. MM is for month.
Those formatting placeholders are fixed. small m is always for minutes. And it's January because this is the default Month value.
mm is for minutes so you do not have any month in your date. Thus, I guess that the month is initialized to 0 (Jan)
The reason it does not fail is because by default the formatter is lenient. If you want it to fail then setLenient(false) on the formatter object.
Although I do not think it will fail in your case as in your example it will read 07 as minutes.
When using
Date from = new SimpleDateFormat("dd/mm/yyyy").parse("05/07/2013");
the simple mm is for minutes therefore i think the value for month is assumed to be 0 so it gives Jan as the default value so use MM for month.
I guess it comes from calendar.clear() which will point to 1 Jan 1970. Then it adds you parsed data = 2013 years (YY), 5 days (dd) and 7 minutes (mm). Use MM for month