Android utc时间

项目中用到的,不是很全,只做备份参考;


1、将utc的秒数,转成现在的时间:


dateTime是个long型的秒数;

long utc1=Long.parseLong(utc);
long utc2=1400000000L;//自己的偏移量 
long utcz=(utc1+utc2)*1000;

public static String getFormatedDateTime(long dateTime) {
       

                  SimpleDateFormat localFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  localFormater.setTimeZone(TimeZone.getDefault());
  String localTime = localFormater.format(new Date(dateTime));
  return localTime;
 }



2、获取当前时间的utc秒数:


//获取时间
SimpleDateFormat fmt = new SimpleDateFormat("yyMMddHHmmss");
fmt.setTimeZone(TimeZone.getTimeZone("Etc/GMT+0"));
String utcTime=fmt.format(new Date());


utcTime就是当前时间获得的utc秒数;


上面那个获得到的uct时间好像时区有问题:

修改:

 private void cfsd(){
 SimpleDateFormat fmt = new SimpleDateFormat("yyMMddHHmmss");
   fmt.setTimeZone(TimeZone.getTimeZone("Etc/GMT+0"));
   String utctime=fmt.format(new Date());
   System.out.println("utc:"+utctime);
 
   SimpleDateFormat localFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     localFormater.setTimeZone(TimeZone.getDefault());
     Long t=151026064432L;//测试
//      Data dd=new Date(t);
     String localTime = localFormater.format(new Date(t));
     System.out.println("转换下utctime==="+localTime);
 
 
 SimpleDateFormat foo = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
     System.out.println("foo:"+foo.format(new Date()));
     
     Calendar gc = GregorianCalendar.getInstance();
     System.out.println("gc.getTime():"+gc.getTime());
     System.out.println("gc.getTimeInMillis():"+new Date(gc.getTimeInMillis()));
     
     //当前系统默认时区的时间:
     Calendar calendar=new GregorianCalendar();
     System.out.print("时区:"+calendar.getTimeZone().getID()+"  ");
     System.out.println("时间:"+calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE));
     //美国洛杉矶时区
     TimeZone tz=TimeZone.getTimeZone("America/Los_Angeles");
     //时区转换
     calendar.setTimeZone(tz);
     System.out.print("时区:"+calendar.getTimeZone().getID()+"  ");
     System.out.println("时间:"+calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE));
     Date time=new Date();
     
     //1、取得本地时间:
     java.util.Calendar cal = java.util.Calendar.getInstance();


     //2、取得时间偏移量:
     int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);


     //3、取得夏令时差:
     int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);


     //4、从本地时间里扣除这些差量,即可以取得UTC时间:
     cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));


     //之后调用cal.get(int x)或cal.getTimeInMillis()方法所取得的时间即是UTC标准时间。
     System.out.println("UTC:"+new Date(cal.getTimeInMillis()));
     
     Calendar calendar1 = Calendar.getInstance();
     TimeZone tztz = TimeZone.getTimeZone("GMT");       
     calendar1.setTimeZone(tztz);
     System.out.println("cc"+calendar.getTime());
     System.out.println("dd"+calendar.getTimeInMillis());//这个地方获得的utc时间,在用utc转成日期,是没有问题的
 
    
 }



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值