以下代码取得的时间比我们时间早8小时,请问是怎么回事?怎么解决,谢谢!!
Calendar ca = Calendar.getInstance(TimeZone.getDefault());
Date date = new Date(System.currentTimeMillis());
ca.setTime(date);
int year=ca.get(ca.YEAR);
int month = ca.get(ca.MONTH);
int day = ca.get(ca.DATE);
int hour = ca.get(ca.HOUR_OF_DAY);
int min = ca.get(ca.MINUTE);
System.out.println(day);
System.out.println(hour);
System.out.println(min);
------解决方案--------------------------------------------------------
时区的原因!
------解决方案--------------------------------------------------------
时区未必都支持
还是直接减8个小时吧
------解决方案--------------------------------------------------------
TimeZone.getDefault()这个有问题,你要设置成北京的时区
------解决方案--------------------------------------------------------
TimeZone.getDefault()默认的时区好象不是北京时区```如果不知道北京时区的表示的话 你可以减8```
------解决方案--------------------------------------------------------
TimeZone timezoneCH=TimeZone.getTimeZone( "China/BeiJing ");
Calendar ca = Calendar.getInstance(timezoneCH);
Date date = new Date(System.currentTimeMillis());
ca.setTime(date);
int year=ca.get(ca.YEAR);
int month = ca.get(ca.MONTH);
int day = ca.get(ca.DATE);
int hour = ca.get(ca.HOUR_OF_DAY);
int min = ca.get(ca.MINUTE);
System.out.println(day);
System.out.println(hour);
System.out.println(min);
------解决方案--------------------------------------------------------
Calendar cal=Calendar.getInstance();
cal.add(Calendar.HOUR,-8);
------解决方案--------------------------------------------------------
TimeZone timezoneCH=TimeZone.getTimeZone( "Asia/Shanghai ");
//这样的问题真麻烦