【问题标题】:How to set String (HH:mm) to UTC time with current date and convert it to local time如何使用当前日期将字符串 (HH:mm) 设置为 UTC 时间并将其转换为本地时间
【发布时间】:2019-11-04 09:47:30
【问题描述】:

我需要将 (HH:mm) 格式的字符串(应该是 UTC 时间)转换为本地时区。如何将当前日期添加到字符串并转换为当地时间。

我尝试过使用日历

String utcTimeString = "06:00";
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Calendar now = Calendar.getInstance(Locale.getDefault());
now.setTime(sdf.parse(utcTimeString));

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 顺便考虑扔掉长期过时且臭名昭著的麻烦SimpleDateFormat和朋友,并将ThreeTenABP添加到您的Android项目中,以便使用java.time,现代Java日期和时间API .使用起来感觉好多了。

标签: java android date calendar datetime-format


【解决方案1】:

建议您将现代 API 用于日期、时间、时区、偏移量、日历等:

java.time

这样做很容易

  1. 解析您收到的时间
  2. 获取当前日期并
  3. 将它们组合成日期时间表示具有特定时区

看这个小例子:

public static void main(String[] args) {
    // create a time object from the String
    LocalTime localTime = LocalTime.parse("06:00", DateTimeFormatter.ofPattern("HH:mm"));
    // print it once in an ISO format
    System.out.println(localTime.format(DateTimeFormatter.ISO_TIME));
    // receive the date of today
    LocalDate today = LocalDate.now();
    // then use the date and the time object to create a zone-aware datetime object
    ZonedDateTime zdt = LocalDateTime.of(today, localTime).atZone(ZoneId.of("UTC"));
    // print it
    System.out.println(zdt.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
}

输出是

06:00:00
2019-11-04T06:00:00Z[UTC]

您可以根据需要使用不同的DateTimeFormatters 对其进行格式化。

【讨论】:

  • 值得注意的是,你不能真正使用java.time API,除非你的目标是 Android API 26 或更高版本,但有一个适用于 Andorid 的反向端口,这里是链接:github.com/JakeWharton/ThreeTenABP
【解决方案2】:

尝试如下。

public String getDateTimeInUTC(String yourTime){ 
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat currentDate= new SimpleDateFormat("MMM dd, yyyy ");

    String currentDateTime = currentDate.format(cal.getTime())+yourTime; // here concate your time with current date.
    System.out.println("Current date with given time: "+currentDateTime);

    SimpleDateFormat df = new SimpleDateFormat("MMM dd, yyyy HH:mm", Locale.ENGLISH);
    df.setTimeZone(TimeZone.getTimeZone("UTC"));

    Date date = null;
    try {
       date = df.parse(currentDateTime);
    } catch (ParseException e) {
       e.printStackTrace();
    }
    df.setTimeZone(TimeZone.getDefault());
    String formattedDate = df.format(date);

    return formattedDate;
}

致电getDateTimeInUTC,如下所示

String strTime = "12:10"; // your string time in HH:mm format
String finalDateTime = getDateTimeInUTC(strTime);
System.out.println("Final date-time in UTC: "+finalDateTime);

输出:

Current date with  given time: Nov 04, 2019 12:10
Final date-time in UTC: Nov 04, 2019 18:10

【讨论】:

    【解决方案3】:

    你可以看看这个:

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    
    //change the format according to your need
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    
    //Here you say to java the initial timezone. This is the secret
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    //Will print in UTC
    System.out.println(sdf.format(calendar.getTime()));    
    
    //Here you set to your timezone
    sdf.setTimeZone(TimeZone.getDefault());
    //Will print on your default Timezone
    System.out.println(sdf.format(calendar.getTime()));
    

    【讨论】:

      猜你喜欢
      • 2011-06-13
      • 2018-02-23
      • 2014-02-22
      • 2012-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-07
      • 2015-10-24
      相关资源
      最近更新 更多