使用java.time
解析您的时间(以秒为单位),它提供了以秒为单位的方法...
public static void main(String[] args) {
// your seconds
long seconds = 1574348400;
// same in millis
long millis = 1574348400000L;
// find out the zone of your system
ZoneId systemDefaultZoneId = ZoneId.systemDefault();
// or set a specific one
ZoneId utcZoneId = ZoneId.of("UTC");
// parse a ZonedDateTime of your system default time zone from the seconds
ZonedDateTime fromSecsSysDefZone = ZonedDateTime.ofInstant(Instant.ofEpochSecond(seconds),systemDefaultZoneId);
// parse a ZonedDateTime of UTC from the seconds
ZonedDateTime fromSecsUtc = ZonedDateTime.ofInstant(Instant.ofEpochSecond(seconds),utcZoneId);
// parse a ZonedDateTime of your system default time zone from the milliseconds
ZonedDateTime fromMillisSysDefZone = ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis),systemDefaultZoneId);
// parse a ZonedDateTime of UTC from the milliseconds
ZonedDateTime fromMillisUtc = ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis),utcZoneId);
// print the ones that were created using your default time zone
System.out.println("from seconds:\t"
+ fromSecsSysDefZone.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
System.out.println("from millis:\t"
+ fromMillisSysDefZone.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
// print a check for equality
System.out.println("Both ZonedDateTimes are "
+ (fromSecsSysDefZone.equals(fromMillisSysDefZone) ? "equal" : "different"));
System.out.println("————————————————————————————————");
// print the ones that were created using UTC
System.out.println("from seconds:\t"
+ fromSecsUtc.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
System.out.println("from millis:\t"
+ fromMillisUtc.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
// print a check for equality
System.out.println("Both ZonedDateTimes are "
+ (fromSecsUtc.equals(fromMillisUtc) ? "equal" : "different"));
}
此代码(在我的系统上)产生的输出为
from seconds: 2019-11-21T16:00:00+01:00[Europe/Berlin]
from millis: 2019-11-21T16:00:00+01:00[Europe/Berlin]
Both ZonedDateTimes are equal
————————————————————————————————
from seconds: 2019-11-21T15:00:00Z[UTC]
from millis: 2019-11-21T15:00:00Z[UTC]
Both ZonedDateTimes are equal
如果必须使用Java 6或7,则可以使用ThreeTenBackport-Project on Github,它在这两个旧版本中启用java.time
的(大多数)功能。
on a separate website说明了其用法。
,
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class BasicWebCrawler {
public BasicWebCrawler() {
long value = 1574348400000L;
Date date = new Date(value);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE,-1);
Date minusOne = cal.getTime();
String dateString = new SimpleDateFormat("EEEE dd MMMM,yyyy").format(minusOne);
System.out.println("Formated time: " + dateString);
}
public static void main(String[] args) {
new BasicWebCrawler();
}
}
输出:格式化时间:2019年11月20日星期三
,
错误的value
。试试:
long value = 1574348400000L;
,
您的第一个问题是:您使用的是秒而不是毫秒,new Date(long)
的值long
以毫秒为单位。
See the Java 6 java.util.Date Documentation here
您的第二个问题是:使用Java 6 Date
时,您需要知道确定毫秒值的位置,如果不在您的时区,则需要进行转换。以下面的代码为例:
String zeroDateString = new SimpleDateFormat("EEEE dd MMMM,yyyy hh:mm").format(new Date(0));
System.out.println("Formated time -- zeroDateString = " + zeroDateString);
在美国纽约州的纽约市,new Date(0)
的输出为Wednesday December 31,1969 19:00
(纽约市的时区为EST,格林尼治标准时间-05:00 ),而在意大利罗马,相同代码的输出为Thursday 01 January 1970 01:00
(意大利罗马的时区为 GMT + 01:00 )
如果您需要所有数据均符合格林尼治标准时间(GMT),那么您将需要根据与时区相关的时间相对于 GMT 进行调整和/或计算。
本文链接:https://www.f2er.com/3067802.html