How to preserve milisecond data on java simpledateFormat - java

Basically I'm trying to convert string data to Timestamp format. When I converted data to timestamp format, SimpleDateFormat added three minutes.
Because milisecond data equals 3 minute.But I want to preserve milisecond data on timestamp value.
Code:
public Double TimestampTest(String Timestamp ){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date parsedDate = null;
try {
parsedDate = dateFormat.parse(Timestamp);
} catch (ParseException e1) {
e1.printStackTrace();
}
Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
...
}
Timestamp value
2002-04-17 23:45:58.983
For test case
System.out.println(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse("2015-04-22T19:54:11.219983Z"));
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS'Z'").parse("2015-04-22 19:54:11.219983Z"));
Results are same for both of them
Wed Apr 22 19:57:50 EEST 2015
Wed Apr 22 19:57:50 EEST 2015
Because
219 983 milliseconds = 3.66638333 minutes
To sum up I want to preserve ms data .Is there any way to do it ?

If I understand, what you have at the end of your string are microseconds, not milliseconds. The old, obsolete Date and SimpleDateFormat classes have no support for microseconds. But you can use the new java.time classes:
System.out.println(ZonedDateTime.parse("2015-04-22T19:54:11.219983Z",
DateTimeFormatter.ofPattern(("yyyy-MM-dd'T'HH:mm:ss.SSSSSSVV"))));

Related

Conversion from GMT to CST showing 7 hrs difference

I have taken this date "2016-04-26 12:00:00”, and converted to GMT and CST epochs, using the function below. I got the dates below. Not sure I am doing anything wrong here.
1461672000000 UTC ——> Tue, 26 Apr 2016 12:00:00 GMT
1461690000000 CST —> Tue, 26 Apr 2016 17:00:00 GMT
Code:
long epoch = 0;
String str = "2016-04-26 12:00:00";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("CST")); //This GMT or CST
Date datenew = df.parse(str); //parsethe date
epoch = datenew.getTime(); //get the epoch time
As eluded to by Erickson in the comments, your expectations seem inverted from the implementation; when you set the TimeZone in the DateFormat, using the DateFormat.parse() method results in the string it's parsing as if it is coming from that TimeZone (and converts it to the local time). So the results you notice are exactly expected.
To fix this, use the DateFormat.format() method instead:
public static void main(String[] args) {
String dateStr = "2016-04-26 12:00:00";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
Date gmtDate = null;
try {
gmtDate = df.parse(dateStr);
}
catch (ParseException e) {
e.printStackTrace();
}
System.out.println("GMT TIME = " + df.format(gmtDate));
df.setTimeZone(TimeZone.getTimeZone("CST"));
System.out.println("CST TIME = " + df.format(gmtDate));
}
Output:
GMT TIME = 2016-04-26 12:00:00
CST TIME = 2016-04-26 07:00:00

Convert from string to UTC format

I am developing an Android app.
In this app I am working with dates.
I need all dates to be in UTC format. I am using this method to convert them:
public Date getConvertedUTCDate(String datetime) {
Date myDate = null;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
try {
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
myDate = dateFormat.parse(datetime);
} catch (ParseException e) {
e.printStackTrace();
}
return myDate;
}
But instead of getting the expected:
Sun Aug 10 14:13:14 UTC 2014
I get it in CEST format
Sun Aug 10 14:13:14 CEST 2014
What am I doing wrong?
You only set the TimeZone for the calendar of the parser. Not the TimeZone of the parsed result.

Date value wrongly formatted

I am trying to convert a String DateTime value which is present in a flat file as a Date object after parsing the flat file in my code.
I have written the code to do that but when I format the date its always giving me a date more than 1 day for the specified value, some times it's adding 5:30.
Below is the code for that:
DateFormat f = new SimpleDateFormat("EEE MMM dd HH:mm:ss zz yyyy");
Date date = f.parse("Tue Aug 23 20:00:03 PDT 2011");
System.out.println("---date----" + date);
The output for the above is
---date----Wed Aug 24 08:30:03 IST 2011
Can you please let me know whats the issue here. Is there a problem in the pattern that I am using in the SimplaDateFormat class or is there a problem with the code.
I have been scratching my head on this for a long time now.
Can you please let me know whats the issue here.
Sure. You're effectively calling date.toString(), which doesn't know anything about the SimpleDateFormat which was used to parse the original text value. A Date is just an instant in time. It has no notion of a per-instance format. Additionally, it doesn't know about a time zone. You've given a value in PDT, which was then parsed... and when you print it, it's using the system local time zone (IST). That's what Date.toString always does.
If you want to format a Date in a particular way, using a particular format in a particular time zone, call DateFormat.format.
Your system timezone is different. The output is showing IST - or Indian Standard Time, which is an 12.5h difference from PDT. The code is properly parsing the given date which is PDT (UTC -7) and printing out IST (UTC +5h30).
Java stores Dates as UTC dates. So when you parse the PDT date, Java will convert it to UTC and store it internally as a UTC timestamp. When you print, if you do not specify the timezone, it will default to the system timezone, which in your case would appear to be IST.
To specify an exact timezone, specify it in the SimpleDateFormat:
DateFormat f = new SimpleDateFormat("EEE MMM dd HH:mm:ss zz yyyy");
f.setTimeZone(TimeZone.getTimeZone("PDT"));
Date date = f.parse("Tue Aug 23 20:00:03 PDT 2011");
System.out.println("---date----" + f.format(date));
Because you are not formatting a date. Look at the example
public static void main(String[] args){
Locale currentLocale = Locale.US;
DateFormat f = new SimpleDateFormat("EEE MMM dd HH:mm:ss zz yyyy", currentLocale);
Date date = null;
Date today;
try {
today = new Date();
String result = f.format(today);
System.out.println("Locale: " + currentLocale.toString());
System.out.println("Result: " + result);
date = f.parse("Tue Aug 23 20:00:03 PDT 2011");
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("---date----" + f.format(date));
}
will output
Locale: en_US
Result: Tue Sep 25 19:12:38 EEST 2012
---date----Tue Aug 23 20:00:03 PDT 2011
Now, you have a bit modified code
public static void main(String[] args){
Locale currentLocale = Locale.US;
DateFormat f = new SimpleDateFormat("EEE MMM dd HH:mm:ss zz yyyy", currentLocale);
DateFormat f2 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zz yyyy", currentLocale);
Date date = null;
Date today;
try {
today = new Date();
String result = f.format(today);
System.out.println("Locale: " + currentLocale.toString());
System.out.println("Result: " + result);
date = f.parse("Tue Aug 23 20:00:03 PDT 2011");
System.out.println("---date----" + f.format(date));
System.out.println("---date----" + f2.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
which outputs to
Locale: en_US
Result: Tue Sep 25 20:42:10 EEST 2012
---date----Tue Aug 23 20:00:03 PDT 2011
---date----Wed Aug 24 06:00:03 EEST 2011
seems that SimpleDateFormat don't care about timezone even if 'z' pattern is specified. It is setting the timezone when it parses the input. That's how I can describe that a strange behavior. Then use of 'z' pattern seems obsolete and lead to unpredictable results.
so setting the TimeZone will fix the issue
f2.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));

I am trying to SimpleDateFormat date in java

I am trying to format a date by parsing it and then formating it but it is not working.
It is showing a parsing exception
public java.util.Date convertFormat(String DateTimeForm)
throws ParseException {
DateTimeForm="2012-06-01 10:00 PM";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aaa");
java.util.Date FCDate = (java.util.Date) formatter.parse(DateTimeForm);
return (java.util.Date) FCDate;
}
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aaa");
try {
Date date = formatter.parse("2012-06-01 10:00 PM");
System.out.println(date.toString());
} catch (ParseException e) {
e.printStackTrace();
}
Didn't change anything and yet it works.
Fri Jun 01 22:00:00 CDT 2012
This works fine on my machine. I didn't change anything important.
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aaa");
Date date = null;
try {
date = formatter.parse("2012-06-01 10:00 PM");
} catch (ParseException ex) {
// Intentionally empty. Failed parse causes date == null.
}
System.out.print(date);
prints
Fri Jun 01 22:00:00 EDT 2012
The Java docs say the numerics are all locale-independent, but not the AM/PM. For example the code fails if you specify Locale.JAPAN in the formatter construction. Specify Local.US to guarantee AM/PM will always work.

Date formatting issue in java

When I parsing time in java, I passing "12:12" as string argument, then I am getting "Thu Jan 01 12:12:00 IST 1970" as a output.
I want current year like "Fri Mar 09 12:12:00 IST 2012" as a output.
String timestr = "12:12";
Date convertedDate = null;
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
convertedDate = dateFormat.parse(timestr);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(convertedDate);
Thanks!
I think that the problem with this is that you are creating a date with null values and then just initialize the time value. I think you should use the Calendar class and get an instance of the Calendar and then set the time. Once that is done, you create a date object from the Calendar and parse it to your needs.

Categories

Resources