java - 如何在Java中获取您所在时区的当前时间

标签 java android eclipse date

我不知道为什么 Java 几乎不可能做这么简单的事情。我已经尝试了很多在网上找到的解决方案,但似乎没有一个简单干净且有效的解决方案。

这是我最近的解决方案尝试

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("America/Los_Angeles"));
System.out.println( sdf.format(calendar.getTime()) );

我的异常输出:27/02/2014 17:06:00

我的真实输出:03/03/2014 20:35:44

这有什么意义。

最佳答案

改为在 SimpleDateFormat 实例中设置时区

sdf.setTimezone("America/Los_Angeles");

关于java - 如何在Java中获取您所在时区的当前时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22084171/

相关文章:

android - 不允许按下按钮产生负值

java - Maven.surefire.util.SurefireReflectionException 在测试 Junit 测试时

java - 什么时候应该优先选择 Callable 而不是 Runnable?为什么?

java - 为什么是 'variable can be only null at this location' ?

java - 计算 build 房屋所需的乐高积木数量

android - 从 sqlite android 排序 ListView 项目

android - 如何以编程方式获得强调色?

java - 我可以手动/编程方式生成 Eclipse 工作区和项目吗?

eclipse - 使用eclipse juno配置Java CV

java - 将IDEA与maven2一起使用,如何添加非maven .jar?