无论时区如何,都以UTC格式获取设备时间

UBA*_*eam 2 java timezone datetime android utc

我怎样才能获得设备的本地时间并将其转换为我国家的全球UTC格式?

Jon*_*eet 5

根本没有得到当地时间 - 只需要开始使用UTC值,例如

long millis = System.currentTimeMillis();
Run Code Online (Sandbox Code Playgroud)

要么:

Date date = new Date();
Run Code Online (Sandbox Code Playgroud)

如果您需要将其格式化为字符串,请使用SimpleDateFormat但请记住正确设置时区.例如:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                                               Locale.US);
format.setTimeZone(TimeZone.getTimeZone("Etc/Utc"));
String text = format.format(new Date());
Run Code Online (Sandbox Code Playgroud)

(目前尚不清楚你所说的"我的祖国全球UTC格式"是什么意思- UTC简直全球性的,它不是一个格式,它是一个时区.)

  • @ user1812264:没有"我的国家的UTC时间" - UTC是全球性的. (2认同)