I get an Date with format: MM-dd-yy hhmma zzz. The problem is that i only get an A for AM or P for PM. How can I parse a string with that format to a date object?
Alex
Use this 'a'
example: MM-dd-yy hhmma a;
Visit http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
This pattern is working for me:
dd-MM-yyyy hh:mm:ss a
It prints the information in this format:
12-02-2016 03:50:07 PM
Simple, do a string replace of A->AM/P->PM before parsing. SimpleDateFormat does not support A/P for AM/PM.
dd-MM-yyyy (MM-stands for month). Please do not use MM in the time section of the DATE TIME hh:MM:ss.
For example. It will display the date '15-02-2017 12:12:05' as
'15-02-2017 12:02:05' instead of 12:12:05. Month replaced in the place of minutes.
hh - stands for 12 hours time format
HH - stands for 24 hours time format
it is best to use 12 hours time format with a 'hh:mm a' to give clear picture on whether it is 'AM/PM'
Related
I am trying to parse following date time string
2018-01-30T23:59:59.000
I am not able to understand which standard format it is like UTC or ISO_8601
while parsing in the following manner:
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD'T'HH:MM:SS:MS");
Date date = null;
try {
date = sdf.parse("2018-01-30T23:59:59.000");
} catch (ParseException e) {
e.printStackTrace();
}
But It is throwing following exception:
java.text.ParseException: Unparseable date: "2018-01-30T23:59:59.000"
Any help is appreciated.
See the doc of SimpleDateFormat and try this:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDateTime dateTime = LocalDateTime.parse("2018-01-30T23:59:59.000");
System.out.println(dateTime);
This prints:
2018-01-30T23:59:59
Your string is in ISO 8601 format. UTC or Coordinated Universal Time is not a format, it is a standard time used to define the time the rest of use in our respective time zones.
The date-time classes you were using, SimpleDateFormat and Date, are long outdated and the former in particular notoriously troublesome. I recommend that you instead use java.time, the modern Java date and time API. It is so much nicer to work with.
A LocalDateTime is a date with time of day and without time zone or offset from UTC. Its one-argument parse method parses ISO 8601, which is why no explicit formatter is needed.
What went wrong in your code
Your format pattern string has a number of issues to it. Which is one reason why you should appreciate the above solution without any explicit formatter. The first thing that goes wrong is: Your format pattern string has a colon, :, between seconds and milliseconds, whereas your date-time string has a dot, .. This is why you get the exception.
However, fixing this, your code yields the following Date:
Sun Dec 31 23:00:00 CET 2017
It’s one month off from the expected, and the minutes and seconds are missing. Because:
Uppercase YYYY is for week-based year and only useful with a week number. You need lowercase yyyy for year.
Uppercase DD is for day of year. You need lowercase dd for day of month.
You correctly used uppercase MM for month. Trying the same again for minutes won’t work. Maybe you can guess by now: it’s lowercase mm.
Not surprising you need lowercase ss for seconds.
UsingMS for milliseconds is interesting. SimpleDateFormat takes it as M for month (which we’ve already had twice before) and uppercase S for millisecond. Instead you needed uppercase SSS for the three digits of milliseconds.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Wikipedia article: ISO 8601
Wikipedia article: Coordinated Universal Time on UTC
You need to escape the literal T:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:SS");
See This SO Answer for more examples
Update: Your string is in the format
yyyy-MM-dd'T'HH:mm:ss.SSS
but you are trying to parse it with a completely uppercase format string.
This does not do what you want it to do and you should read the documentation on SimpleDateFormat and the format string placeholders
I am converting a timestamp from "1999-02-18 11:30:00.0" to "1999-02-18 11:30:00". Well, simple enough. So I use following:
newForm = new SimpleDateFormat('yyyy-mm-dd hh:mm:ss').format(oldForm)
println oldForm
println newForm
However, the print out shows the value of the timestamp is changed.
oldForm 1999-02-18 11:30:00.0
newForm 1999-30-18 11:30:00
This really drive me crazy. Is this a bug or what? Anyother way to do it? I really don't want to do string trimming
You should use (capital) MM for month; mm is minutes, which is 30 here.
Date Format Symbols
mm is not the Month. You should do this:
yyyy-MM-dd hh:mm:ss
mm is minutes. MM is month. all fixed.
You are using 'mm' instead of 'MM'
mm is for minutes
MM is for month
Also check this page for more details
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
It will help you whenever you will be dealing with SimpleDateFormat
How do i convert the following String Date into Data format in Java?
"10/01/2012 06:45:23:245946"
I am using the following code
dateFormat = new SimpleDateFormat("MM/dd/yyyy hh24:mm:ss:SSS");
java.util.Date parsedDate = dateFormat.parse("10/01/2012 06:45:23:245946");
And i am getting the following error
java.text.ParseException: Unparseable date: "10/01/2012 06:45:23:245946"
There is no hh24 in SimpleDateFormat, You should be using HH
Your pattern is wrong. Try:
"MM/dd/yyyy HH:mm:ss:SSS"
There is no hh24 in date matching pattern.
The pattern for hour is as follows:
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
See the whole date pattern on SimpleDateFormat javadoc.
You're almost there.
Get rid of the 24 after hh and change it to HH, that should make it work.
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss:SSS");
Date parsedDate = dateFormat.parse("10/02/2012 06:45:23:245946");
System.out.println(parsedDate);
This will give you an error in time but parse the date successfull, as will all of our answers.
This is fixed by trimming the milliseconds down to 3 digits from 245946 to 245
If you do however want to use 6 digits I would suggest looking into the JodaTime API for more advanced datehandling as JodaTime handles microseconds. But as for java.util.Date, you're out of luck I'm afraid.
Read this bugreport why:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4148168
EDIT: Thanks Jesper for pointing out my bad wording
The 24 in your date format is an invalid format specifier. Remove it. HH is the equivalent of hours on a 24-hour scale.
dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss:SSS");
SimpleDateFormat dateFormat = new SimpleDateFormat("mm/dd/yyyy");
Date date = dateFormat.parse("11/04/2011");
System.out.println(date);
System.out.println(dateFormat.format(date));
Tue Jan 04 00:11:00 IST 2011
11/04/2011
When output the date object it shows as january instead of november. But when format the same date object it shows november correctly.
By making MM/dd/yyyy it both shows the correct result. But Shouldnt the mm/dd/yyyy throws an Unparseable date exception if mm/dd/yyyy is fishy?
A common error: mm is the pattern for minutes. You want to use MM to parse/print the month. Use MM/dd/yyyy instead of mm/dd/yyyy.
Since you parse the value 11 into the minute-part of the date and then print the minute-part of the date in the first place, the result of dateFormat.format(date) looks correct.
mm/dd/yyyy change it to MM/dd/yyyy
Letter Date or Time Component Presentation Examples
M Month in year Month July; Jul; 07
m Minute in hour Number 30
See
API Doc
Please note that Date is deprecated. You should use the Calendar class instead.
It is not fishy, capitalized M means Month in year and uncap m means Minute in hour
consult to this http://download.oracle.com/javase/6/docs/api/index.html?java/text/SimpleDateFormat.html
I'm trying to put a user provided date into an SQL database, and I have the following lines to process the string and convert it to a java.sql.Timestamp object.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd'T'hh:mm");
java.util.Date badDate = formatter.parse(request.getParameter("datetime"));
Timestamp date = new Timestamp(badDate.getTime());
The issue is, badDate is the correct date that the user input, but date always gets set to the wrong month and day, usually January 2, and the correct time and year. I had the same problem when I tried to convert to a java.sql.Date object, but then the time was set to midnight as well. I couldn't find anyone with a similar problem after some searching, maybe someone here has seen something like this?
The date format is wrong, it should be yyyy-MM-dd'T'HH:mm.
Please refer this document for details about date format.
m stands for Minutes
M for Months
h for hours in am/pm (1-12)
H for hours in 24 hour clock
It will be better if you can give a sample of the date value you are trying to parse.
You want "yyyy-MM-dd'T'hh:mm" (note capitalized MM). Otherwise your month is wrong in the format string.