找出两个utc时间之间的差异

时间:2013-11-13 21:38:54

标签: android time utc

我试图创建一个以2013-11-05T16:44:54.128515形式获取utc时间的函数,并获取当前的utc时间并进行比较。它应该返回小于24小时的差异,如果小于24小时则返回天数

        public String time_parser(String time)
  {
      Date d1 = null;
        Date d2 = null;
        // DateFormat df = DateFormat.getTimeInstance();
        //df.setTimeZone(TimeZone.getTimeZone("utc"));
        //String gmtTime = df.format(new Date());
         //  Log.d("response ok", "TIME TIME TIME  " +  gmtTime);
        String timesince="";

         final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
         sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
         final String utcTime = sdf.format(new Date());


        try{
            d1=sdf.parse(time);

            d2= sdf.parse(utcTime);
            long diff = d2.getTime()-d1.getTime();
            long diffSeconds = diff / 1000 % 60;
            long diffMinutes = diff / (60 * 1000) % 60;
            long diffHours = diff / (60 * 60 * 1000) % 24;
            long diffDays = diff / (24 * 60 * 60 * 1000);
            if(diffDays >1)
            {
                timesince =  Long.toString(diffDays );
                 Log.d("response ok", "TIME=   " + timesince);
            }
            else
            {
                   timesince =  Long.toString(diffHours);
                   Log.d("response ok", "TIME 2=   " + timesince);
            }

        }
        catch (Exception e) {
            e.printStackTrace();
        }


      return  timesince;
  }

0 个答案:

没有答案