I am aware that SimpleDateFormat.parse
rely on the Calendar API which depends on local JVM timezone (computer's). Assume JVM timezone is IST.
SimpleDateFormat srcDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
srcDateFormat.setTimeZone(TimeZone.getTimeZone("EST"));
Date objDt = srcDateFormat.parse("2018-10-16 11:28:25"); //Time : 21:58:25
From the output it seems it converts from EST to IST(JVM local timezone).
SimpleDateFormat srcDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
srcDateFormat.setTimeZone(TimeZone.getTimeZone("IST"));
Date objDt = srcDateFormat.parse("2018-10-16 11:28:25"); //Time : 11:28:25
It keeps time unmodified in this case. In this case I set timezone same as JVM local timezone.
Please help me to understand the behavior of the parse
method. Nevertheless, I am curious to know the reason behind such behavior.
I know that java.util.Date
and java.text.SimpleDateFormat
legacy classes are obsolete now.
References:
- Why SimpleDateFormat.format() and SimpleDateFormat.parse() are giving different time though setting only one TimeZone?
- How do I convert date/time from one timezone to another?
- SimpleDateFormat parses a string to wrong time
- https://www.codeproject.com/Tips/1190426/When-Parsing-Formatting-Dates-in-Java-Make-Sure-Yo
"1,23"
to a double, and print the double, it will always be printed as1.23
, because the default way of formatting a double is to use the English format (i.e. using a dot and not a comma as decimal separator). – JB Nizet