我使用此代码将日期字符串转换为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
我失去了一天!
答案 0 :(得分:2)
如何打印Calendar
?
我认为你得到GMT时间,落后一小时,因此在午夜落后一天。
考虑:
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
现在,如果我向TimeZone
添加DateFormat
:
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
在格式化时使用默认TimeZone
,而TimeZone
的{{1}} (因为您调用Calendar
}})。
答案 1 :(得分: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
如何打印日期?
答案 2 :(得分: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
答案 3 :(得分:0)
问题是你如何得到30.3.2012 00:18。日历日期字段正常,请尝试此
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
答案 4 :(得分: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());
输出:
Sat Mar 31 03:48:10 IST 2012