Java每次尝试都会给我错误的时间

时间:2014-09-25 15:58:34

标签: java android datetime

我需要在Android应用程序中获得正常的正确时间。但每次我运行应用程序时,我都会错误地分钟错误。而且这不是错误的TimeZone。每次运行应用程序时,时间都不同!我在这里搜索了一些问题,但没有找到答案。没有什么可以解决这个问题...为什么它在java中如此成问题?在c#中。例如,你可以通过写作来获得时间:var time = DateTime.Now。我怎样才能获得正常的正确时间?我计算机上的时区设置为UTC +02:00。 (乌克兰,基辅)。 我的代码:

Calendar curTime = new GregorianCalendar();
curTime.setTimeZone(TimeZone.getTimeZone("Ukraine/Kiev"));
DateFormat dateFormat = new SimpleDateFormat("HH:ss");
dateFormat.setCalendar(curTime);

String time = dateFormat.format(curTime.getTime());

2 个答案:

答案 0 :(得分:2)

This answer在您的getTimeZone方法中使用了一些时区。

Calendar curTime = new GregorianCalendar();
curTime.setTimeZone(TimeZone.getTimeZone("Europe/Kiev"));
DateFormat dateFormat = new SimpleDateFormat("HH:mm");
dateFormat.setCalendar(curTime);

String time = dateFormat.format(curTime.getTime());
System.out.println(time);

输出: 19:22

答案 1 :(得分:0)

我希望这应该有用..时区是GMT + 3乌克兰

  Date today = Calendar.getInstance().getTime();
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-hh.mm.ss");
  sdf.setTimeZone(TimeZone.getTimeZone("GMT+3"));
  String date = sdf.format(today);

以上代码::

的输出
  2014-09-25-09.51.23