I am having a date string 2012-11-21 13:11:25
which I get from local database. I have to convert this according to UTC
settings and display it on a particular screen. So if its GMT+05:30
it should be displayed as 2012-11-21 18:41:25
on the screen. How can I do this conversion. I have checked some of the questions but that didn't work out.
I am able to get a Date object that returns something like Wed Nov 21 13:11:25 GMT+05:30 2012
after this I need to get the time as 18:41:25
and date as 11-21-2012
Thanks in advance
var now = new Date(); var utc = new Date(now. getTime() + now. getTimezoneOffset() * 60000);
UTC, or Universal Time Coordinated, is the most precise and commonly referred to time standard. Since the 1970s, this time standard has been globally used as the most precise time standard, instead of formerly used GMT standard, which has turned now into a regular time zone.
Getting the UTC timestamp Use the datetime. datetime. now() to get the current date and time. Then use tzinfo class to convert our datetime to UTC.
Your df
and inputFmt
must use the same format.
But I think you should do it like this:
Date myDate = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
calendar.setTime(myDate);
Date time = calendar.getTime();
SimpleDateFormat outputFmt = new SimpleDateFormat("MMM dd, yyy h:mm a zz");
String dateAsString = outputFmt.format(time);
System.out.println(dateAsString);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With