Android convert date and time to milliseconds

后端 未结 10 2176
长发绾君心
长发绾君心 2020-11-28 12:05

I have one date and time format as below:

Tue Apr 23 16:08:28 GMT+05:30 2013

I want to convert into milliseconds, but I actually dont know

10条回答
  •  一生所求
    2020-11-28 12:13

    Covert date and time string to milliseconds:

     public static final String DATE_TIME_FORMAT = "MM/dd/yyyy HH:mm:ss a";
    

    or

      public static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
    

    or

      public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZZZZZ";
      //TimeZone.getAvailableIds() to list all timezone ids
      String timeZone = "EST5EDT";//it can be anything timezone like IST, GMT.
      String time = "2/21/2018 7:41:00 AM";
    
     public static long[] convertTimeInMillis(String dateTimeFormat, String timeZone, String... times) throws ParseException {
    
       SimpleDateFormat dateFormat = new SimpleDateFormat(dateTimeFormat, Locale.getDefault());
       dateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
       long[] ret = new long[times.length];
       for (int i = 0; i < times.length; i++) {
          String timeWithTZ = times[i] + " "+timeZone;
          Date d = dateFormat.parse(timeWithTZ);
          ret[i] = d.getTime();
        }
      return ret;
    
    }
    

    //millis to dateString

      public static String convertTimeInMillisToDateString(long timeInMillis, String DATE_TIME_FORMAT) {
         Date d = new Date(timeInMillis);
         SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
         return sdf.format(d);
      }
    

提交回复
热议问题