Convert from string to UTC format - java

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.

Related

How to preserve milisecond data on java simpledateFormat

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

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

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.

How to change the date format in Ireport 4.5

I have given text field expression new.java.util.Date() and pattern MMMMM dd, yyyy as the mentioned format.
The date must display like: jan 13, 2012 but it's displaying in some other format: Fri Jan 13 08:30:12 IST 2012.
So how to print the date in the mentioned format. And one thing in preview the date displays correctly as mentioned but inside my application it displays Fri Jan 13 08:30:12 IST 2012 format. Is there any way to make it to work properly?
new SimpleDateFormat("MMM dd, yyyy ").format(new Date())
Put the above line in text field so you will get your Date format
Use this below method..hope it will help to you
public static String getDateTimeForUgcServer(String date)
{
SimpleDateFormat intputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dt = new Date();
try
{
dt = intputFormat.parse(date);
}
catch (ParseException e)
{
e.printStackTrace();
}
SimpleDateFormat outputFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String str = outputFormat.format(dt);
return str;
}

Categories

Resources