我试图在我的应用程序中获取 UTC 时间,但不幸的是每次我获取当前模拟器日期和时间而不是 UTC 时。
尝试过,
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
ZonedDateTime utcTime = ZonedDateTime.now(ZoneOffset.UTC);
DateTime now = DateTime.now(DateTimeZone.UTC);
我的代码:
//create UTC time
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
System.out.println("DATETIME ==> " + cal.getTime());
ZonedDateTime utcTime = ZonedDateTime.now(ZoneOffset.UTC);
System.out.println("DATETIME = " + utcTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
DateTime now = DateTime.now(DateTimeZone.UTC);
System.out.println("DATETIME = " + now);
非常感谢任何帮助。
最佳答案
试试这个代码:
private String getCurrentDateTimeAccordingToUTC(String format) {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return simpleDateFormat.format(date);
}
String date = getCurrentDateTimeAccordingToUTC("dd-MM-yyyy hh:mm:ss a");
Log.e("date--","inUTC--:"+date);
关于java - 如何在java/android中获得完美的UTC时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68539560/