简体   繁体   中英

GPS time in millis to UTC format

Is there any direct way of converting a certain amount of seconds starting on January 6th 1980 to an UTC format like YYYY/MM/DD HH:mm:ss?

EDIT: Here is the code working:

private Date gpsInit;
//...
try{
        String ds = "06/01/1980";
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        gpsInit = df.parse(ds);
    }
    catch(ParseException e) {
        System.out.println("Unable to parse January 6th 1980");
    }

    textField = new JFormattedTextField(format);
    textField.setText(formatUTC(min));
//...
private String formatUTC(int timeValue) {
        long diference = timeValue + gpsInit.getTime()/1000;
        String pattern = "yyyy/MM/dd HH:mm:ss";
        SimpleDateFormat utcFormat = new SimpleDateFormat(pattern);
        Date utcNow = new Date(diference*1000);
        return utcFormat.format(utcNow);
    }

Something like:

  1. Construct a Date object (via Calendar ) for the 06.01.1980 00:00:00 date
  2. call getTime() to get the milliseconds since EPOCH
  3. Add your milliseconds to that
  4. Construct a new Date object with that milliseconds value.
  5. Format it however you want ( SimpleDateFormatter for example)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM