日期转换问题

 public class GetTime {

public static void main(String[] args) {
try {
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date date = formatter.parse(getGreenWich());
Calendar now = Calendar.getInstance();

changeDate(getGreenWich());
System.out.println("本地时间:" + formatter.format(now.getTime())
+ " " + "格林时间:" + formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}

/**
* 将当前时间转换为格林时间
*
* @return greenWich
*/
public static String getGreenWich() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long mTime = System.currentTimeMillis();
// 得到一个日历使用的默认时区和场所。时区。
int offset = Calendar.getInstance().getTimeZone().getRawOffset();
Calendar c = Calendar.getInstance();
c.setTime(new Date(mTime - offset));
String greenWich = sdf.format(c.getTime());
return greenWich;
}

/**
* 将格林时间转换为当前时间
*
*
*/
public static void changeDate(String var) throws ParseException {

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(var);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);

GregorianCalendar ca = new GregorianCalendar(
TimeZone.getTimeZone("GMT 00:00"));
ca.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR),
calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
format.setTimeZone(TimeZone.getDefault());
String localDate = format.format(ca.getTime());

System.out.println("格林时间:" + var+" "+ "本地时间:" + localDate);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值