Java 时间 : starts at 1th January 1970 at 1am?

标签 java date time

我错过了什么? Date() 是自 1970 年 1 月 1 日午夜以来经过的毫秒数。午夜不应该从凌晨 0 点开始吗?

引用资料:

我的测试程序:

package be.test.package.time;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class TimeWork {

    public static void main(String[] args) {    

        List<Long> longs = new ArrayList<>();
        List<String> strings = new ArrayList<>();

        DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss.SSS");

        //Now
        Date now = new Date();
        strings.add(formatter.format(now));

        //Test dates
        strings.add("01-01-1970 00:00:00.000");
        strings.add("01-01-1970 01:00:00.000");
        strings.add("31-11-1969 00:00:00.000");
        strings.add("01-01-2014 00:00:00.000");

        //Test data
        longs.add(-1L);
        longs.add(0L);
        longs.add(1L);
        longs.add(7260000L);
        longs.add(1417706084037L);
        longs.add(-7260000L);

        //Show the long value of the date
        for (String string: strings) {
            try {
                Date date = formatter.parse(string);
                System.out.println("Formated date : " + string + " = Long = " + date.getTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        //Show the date behind the long
        for (Long lo : longs) {
            Date date = new Date(lo);
            String string = formatter.format(date);
            System.out.println("Formated date : " + string + " = Long = " + lo);        
        }
    }
}

这是结果:

Formated date : 05-12-2014 08:54:59.318 = Long = 1417766099318
Formated date : 01-01-1970 00:00:00.000 = Long = -3600000
Formated date : 01-01-1970 01:00:00.000 = Long = 0
Formated date : 31-11-1969 00:00:00.000 = Long = -2682000000
Formated date : 01-01-2014 00:00:00.000 = Long = 1388530800000
Formated date : 01-01-1970 12:59:59.999 = Long = -1
Formated date : 01-01-1970 01:00:00.000 = Long = 0
Formated date : 01-01-1970 01:00:00.001 = Long = 1
Formated date : 01-01-1970 03:01:00.000 = Long = 7260000
Formated date : 04-12-2014 04:14:44.037 = Long = 1417706084037
Formated date : 31-12-1969 10:59:00.000 = Long = -7260000

为什么是:

Formated date : 01-01-1970 01:00:00.000 = Long = 0

现在是凌晨 1 点。我预计凌晨 0 点。

最佳答案

UTC 时间 1970 年 1 月 1 日午夜。您需要像这样的 TimeZone

public static void main(String[] args) {
    TimeZone tz = TimeZone.getTimeZone("UTC");
    Calendar cal = Calendar.getInstance(tz);
    cal.setTimeInMillis(0);
    DateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss.SSS");
    sdf.setTimeZone(tz);
    System.out.println(sdf.format(cal.getTime()));
}

输出是

01-01-1970 12:00:00.000

关于Java 时间 : starts at 1th January 1970 at 1am?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27311333/

相关文章:

date - 如何在Dart/Flutter中获取当前时间的GMT时间偏移值

ruby - 将字符串日期时间转换为 Ruby 日期时间

javascript - 为 Google 图表时间戳转义 "GMT"

time - 在 Go 中将 UTC 时间转换为 "local"时间

Java 无法识别一台计算机上日志文件中的换行符\n

java - 抽象类中的静态方法

java - 具有投影的 Hibernate 条件不执行 @OneToMany 映射查询

java - 通过加减毫秒数得到正确的日期

php - Timediff计算where查询

java - 执行 psexec 并输出到本地主机上的文件