1

我使用此代码将日期字符串转换为 unix 时间戳:

int year = 2012;
int month = 2; // eg. for march
int day = 31;
int hrs = 0;
int min = 18;

这应该用英文表示这个日期/时间 31.3.2012 00:18 或 3/31/2012 00:18

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin")); // GMT+1
cal.set(year, month, day, hrs, min);
unixtime = cal.getTimeInMillis() / 1000;

unixtime 的结果是:1333145929 如果我把它转换回来(cal.setTimeInMillis(1333145929 * 1000);)我得到30.3.2012 00:18

我输了一天!

4

5 回答 5

2

你是怎么打印出来的Calendar

我想你得到的是格林威治标准时间,比午夜晚一小时,因此晚一天。

考虑:

public static void main(String[] args) throws Exception {
    int year = 2012;
    int month = 2; // eg. for march
    int day = 31;
    int hrs = 0;
    int min = 18;
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin")); // GMT+1
    cal.set(year, month, day, hrs, min);
    long time = cal.getTimeInMillis();
    System.out.println(time);
    cal.setTimeInMillis(time);
    final DateFormat sdf = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.FULL, SimpleDateFormat.FULL);
    System.out.println(sdf.format(cal.getTime()));
}

输出:

1333145915825
Friday, 30 March 2012 23:18:35 o'clock BST

现在,如果我添加TimeZoneDateFormat

final DateFormat sdf = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.FULL, SimpleDateFormat.FULL);
sdf.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
System.out.println(sdf.format(cal.getTime()));

我得到:

1333145887761
Saturday, 31 March 2012 00:18:07 o'clock CEST

因此,在格式化时SimpleDateFormat使用您的默认值,而不是(如您所说的)。TimeZoneTimeZoneCalendarCalendar.getTime()

于 2013-07-30T11:00:30.937 回答
0

问题是您如何获得 30.3.2012 00:18。日历 DATE 字段没问题,试试这个

    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin")); // GMT+1
    cal.set(year, month, day, hrs, min);
    long unixtime = cal.getTimeInMillis() / 1000;
    cal.setTimeInMillis(unixtime * 1000);
    System.out.println(cal.get(Calendar.DATE));

输出

31
于 2013-07-30T10:58:52.203 回答
0

我试过这段代码:

    int year = 2012;
    int month = 2; // eg. for march
    int day = 31;
    int hrs = 0;
    int min = 18;

    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin")); // GMT+1
    cal.set(year, month, day, hrs, min);
    long unixtime = cal.getTimeInMillis() / 1000;

    System.out.println(cal.getTime());

    System.out.println(unixtime);

    cal.setTimeInMillis(unixtime * 1000);

    System.out.println(cal.getTime());

输出是这样的:

Sat Mar 31 00:18:04 CEST 2012
1333145884
Sat Mar 31 00:18:04 CEST 2012
于 2013-07-30T10:57:47.103 回答
0
    int year = 2012;
    int month = 2; // eg. for march
    int day = 31;
    int hrs = 0;
    int min = 18;


    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin")); // GMT+1
    cal.set(year, month, day, hrs, min);
    long unixtime = cal.getTimeInMillis();

    cal.setTimeInMillis(unixtime);

    //Lets print it!
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    sdf.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
    System.out.println(sdf.format(cal.getTime()));

对我来说,上面的代码打印:03/31/2012 00:18:56

你如何打印你的日期?

于 2013-07-30T10:55:47.623 回答
0
int year = 2012;
        int month = 2; // eg. for march
        int day = 31;
        int hrs = 0;
        int min = 18;

        Calendar cal = Calendar.getInstance(TimeZone
                .getTimeZone("Europe/Berlin")); 
        cal.set(year, month, day, hrs, min);
        long unixtime = cal.getTimeInMillis() / 1000;
        cal.setTimeInMillis(unixtime * 1000);
        System.out.println(cal.getTime());

输出:

2012 年 3 月 31 日星期六 03:48:10 IST

于 2013-07-30T11:01:04.577 回答