SimpleDateFormat().format() changed the timestamp value? - java

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

Related

Java SimpleDateFormat am/pm

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'

How do I convert the following String Date into Java Data format in Java?

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");

How to modify my code to only get the time portion of a date?

I've got the following code:
Date time = new SimpleDateFormat("HH:mm").parse("8:00");
When I call time.toString(), the following is produced:
Thu Jan 01 08:00:00 CET 1970
Is there any way I can extract just the 8:00 from it? I have searched far and wide and have not found any way to do it using the standard SimpleDateFormat.
When I call time.toString(), the following is produced
Yes, it would be - because you're calling Date.toString. A Date value has no concept of format.
Is there any way I can extract just the 8:00 from it?
Whenever you want to convert to a string, you should use a DateFormat. So use the same format that you parsed in.
Alternatively, use Joda-Time, which has a LocalTime type specifically for "time of day", and has a handy parse method. You should still use a formatter every time you want to convert to a string, but at least the value will be easier to work with and more descriptive before then.
LocalTime localTime = LocalTime.parse("8:00");
To format this, you can use something like ISODateTimeFormat.hourMinute() or if you might have more precision, perhaps ISODateTimeFormat.hourMinuteSecond() - see the docs for all of the many options available.
recycle your original SimpleDateFormat Object
SimpleDateFormat format = new SimpleDateFormat("HH:mm")
Date time = format.parse("8:00");
String outString = format.format(time);
in case you were wondering, Here's some more information on DateTime Masks
Use the same SimpleDateFormat instance to format date into string.
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Date time = sdf.parse("8:00");
System.out.println(sdf.format(time));
This will print:
08:00
java.util.Date class represents a specific instant in time, with millisecond precision.
API says java.util.Date.toString()
Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy
In order to format date's use SimpleDateFormat class
System.out.println(new SimpleDateFormat("HH:mm").format(time));

Issue converting from java.util.Date to java.sql.Timestamp

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.

SimpleDateParser produces incorrect date?

Good morning! I've been working with the following bit of code for the last two hours, scouring forums, Google and the JDK 1.6 docs for any idea what is going on but can't seem to make this work. I would expect the code to output 07/25/2010 11:59:33 PM but what I get instead is 01/25/2010 11:59:33 PM .
String dateString = "07/25/2010 11:59:33 PM";
DateFormat format = new SimpleDateFormat("MM/DD/yyyy hh:mm:ss a");
Date testDate = format.parse( dateString );
System.out.println(format.format(testDate));
Thinking that it may be something to do with the JDK, I tried writing the same thing using Joda-Time and get the same results. Am I missing something with how DateFormat works?
Thanks in advance for any insight you can provide!
The problem is the "DD" in your format string - it should be "dd".
"DD" means "day of year"... so you're talking about the 25th day of the year, but in the month of July... the "day of year" is taking priority, so you're getting January 25th instead.
DD means day in year as in a Julian day. A Julian day can describe any day in the year, so the month is ignored. Use dd instead (day in month) and you'll get the right result.
U have to take care on case sensitive issues for the formatter. They are a bit weird organised.
Try it with
DateFormat format = new SimpleDateFormat("MM/DD/yyyy hh:mm:ss a");
EDIT: DD means Day in year, dd = day in month
Refering to the JAVA 1.6 API SimpleDateFormat

Categories

Resources