我想将joda设置DateTime为今天凌晨2点(参见下面的示例代码).但我得到了这个例外:
Exception in thread "main" org.joda.time.IllegalFieldValueException: Value 2 for hourOfDay is not supported: Illegal instant due to time zone offset transition: 2011-03-27T02:52:05.239 (Europe/Prague)
at org.joda.time.chrono.ZonedChronology$ZonedDateTimeField.set(ZonedChronology.java:469)
at org.joda.time.MutableDateTime.setHourOfDay(MutableDateTime.java:702)
Run Code Online (Sandbox Code Playgroud)
上面处理异常的正确方法是什么,或者DateTime在一天中的特定时刻创建一个?
示例代码:
MutableDateTime now = new MutableDateTime();
now.setHourOfDay(2);
now.setMinuteOfHour(0);
now.setSecondOfMinute(0);
now.setMillisOfSecond(0);
DateTime myDate = now.toDateTime();
Run Code Online (Sandbox Code Playgroud)
谢谢.
在我的Java程序中,我需要创建当前时刻的实例.我用
Date date = new Date();
Run Code Online (Sandbox Code Playgroud)
这根据主机的系统时钟给出了当前日期和时间.有什么方法可以从在线服务器获取当前日期和时间吗?也许是世界时间服务器?
我看过这篇文章,它描述了我想要的东西,但我担心我需要的帮助比那里提供的更多.
简而言之,我想获得一个不依赖于主机系统时钟的日期和时间.
谢谢!
我在互联网上做了一些研究但仍然困惑.UNIX时间通用时间是否像GMT/UTC一样,或者它是否因地方时间而异地变化?
我知道UNIX时间从1970年1月1日00:00:00 GMT算起.当我在Java中使用getTime()函数时(更具体地说,日期d = new Date(); long currentTime d.getTime())我以毫秒为单位获取UNIX时间.现在如果A人和B人使用相同的功能坐在两个不同的时区,他们会得到相同的结果吗?
类似的问题可能会被问很多次,但我找不到在 UTC 时间要求人类可读格式的问题。
我想将 currentTimeMillis() 转换为人类可读格式,例如“YYMMDDHHMMSS”。我正在尝试使用 SimpleDateFormat 但它返回 LocalTime 而不是 UTC。
long currentTimeMillis = System.currentTimeMillis();
DateFormat dateFormat = new SimpleDateFormat("yyMMddHHmm");
Date date = new Date(currentTimeMillis);
String currentTime = dateFormat.format(date);
Run Code Online (Sandbox Code Playgroud)
这段代码给了我 LocalTime 但我只想要 UTC 时间。
我想得到确切的当前时间.我不想要系统时间,因为任何人都可以改变它,我想在不访问互联网的情况下这样做,有办法吗?