How to convert a time to UTC and then device local time

冷暖自知 提交于 2019-12-24 20:05:34

问题


I am getting following time from service

'Nov 11 2019 7:30 PM'

I know this is US Central Time, I need to get hours between current time and this event time, but i am unable to understand how to convert this date to UTC.

I am using following approach, but this does not seem to be working fine.

   public Date ticketJSONDateFormatter(String dateTime){

            SimpleDateFormat simpleDateFormatter = new SimpleDateFormat("MMM d yyyy HH:mm a");

            Date parsedDate= null;
            try {

                simpleDateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
                parsedDate= simpleDateFormatter.parse(dateTime);

           } catch (ParseException e) {
                e.printStackTrace();
            }

            return parsedDate;
        }   

This method returns following date

Fri Oct 11 12:30:00 GMT+05:00 2019

Although the expected output may be something like this. My device is at (+5:00 UTC)

Fri Oct 12 12:30:00 GMT+05:00 2019

回答1:


You can use `LocalDateTime' to parse the string to date,

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd yyyy hh:mm a");
String date = "Nov 11 2019 07:30 PM";
LocalDateTime ldt = LocalDateTime.parse(date, formatter);

Then convert it to your preferred zone,

Instant cdt = ldt.atZone(ZoneId.of("America/Chicago")).toInstant();
return cdt.atOffset(ZoneOffset.UTC)

This will return an Instant.

And as Ole V.V suggested in the comment, I wouldn't recommend using old Date and Calendar API. I would suggest reading this answer to understand the issues associated with the old Date API.




回答2:


You can get this in following steps:

  1. Use ZonedDateTime.parse to parse the time you are receiving.
  2. Convert the America Central time to your local time.
  3. Get your current time.
  4. Find the difference between your current time and the event time converted to your local.

Example:

    // Parsing the time you are receiving in Central Time Zone. Using Chicago as a representative Zone.
    String dateWithZone = "Nov 11 2019 7:30 PM".concat("America/Chicago") ;
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd uuuu h:m aVV");

    ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateWithZone, formatter);
    System.out.println(zonedDateTime); // This is the time you received in Central time zone.

    // Now convert the event time in your local time zone
    ZonedDateTime eventTimeInLocal = zonedDateTime.withZoneSameInstant(ZoneId.systemDefault());

    // Then find the duration between your current time and event time
    System.out.println(Duration.between(ZonedDateTime.now(), eventTimeInLocal).toHours());

The duration class provides many other utilities methods to get more precise duration.



来源:https://stackoverflow.com/questions/58327330/how-to-convert-a-time-to-utc-and-then-device-local-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!