如何使用joda日期时间转换UTC格式的字符串日期

问题描述 投票:0回答:1
 public static String convertInDateTimeSecondTOJodaTime(String dateTime) {
        try {
            DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
            DateTime date = formatter.parseDateTime(dateTime).withZoneRetainFields(DateTimeZone.UTC);
            return date.toString("h:mm aa");
        } catch (Exception e) {
            return null;
        }
    }

main(){
print(convertInDateTimeSecondTOJodaTime("2020-04-09T07:31:16Z"))
}

我正在尝试使用joda日期时间将给定的日期时间转换为UTC格式,给了错误的时间,给了一个小时,然后请帮助我做错了。

java android jodatime
1个回答
0
投票
import java.util.Date;
import java.util.TimeZone;
import java.text.SimpleDateFormat;

public class CurrentUtcDate {
    public static void main(String[] args) {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        System.out.println("UTC Time is: " + dateFormat.format(date));
    }
}

Output
UTC Time is: 22-01-2018 13:14:35

您可以在这里查看https://www.quora.com/How-do-I-get-the-current-UTC-date-using-Java

© www.soinside.com 2019 - 2024. All rights reserved.