Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a date and time string according to UTC format

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

like image 306
Lavanya Avatar asked Nov 20 '12 12:11

Lavanya


People also ask

How do you convert date to UTC format?

var now = new Date(); var utc = new Date(now. getTime() + now. getTimezoneOffset() * 60000);

What is UTC time string?

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.

How do you convert datetime to UTC time in python?

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.


1 Answers

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);
like image 146
Kai Avatar answered Sep 23 '22 02:09

Kai