0

I am trying to convert a GMT time which is stored as epoch into CST.

Below is the current code I am running in java.

        if (originalRFC.get("sysmodtime")!=null){    
        var sysmod = originalRFC.get("sysmodtime");                                 // Hold the sysmodtime value in a variable

        logger.debug('Sysmodtime Before: ' + sysmod);                               // Output to log before before converstion to CST - (in GMT)

        var format = new java.text.SimpleDateFormat("MMMM d, yyyy HH:mm:ss a z");   // Format the string will be in        
        var dateString = sysmod.toLocaleString();                                   // Convert the epoch to the string equivalent in CST 
        var parsedDate = format.parse(dateString);                                  // Convert that CST String back to epoch 
        var sysmodConvert = parsedDate.getTime();                                   // Convert it to time and milliseconds

        logger.debug('Sysmodtime After: ' + sysmodConvert);                                 //Output to log after conversion to CST

        genericRFC.setField("last-update-time",sysmodConvert);
}

See the below errors that are returned in the log, we can see the time before"1301382996000", and it breaks when I try to convert:

2011-05-02 14:25:49,926 [http-8080-1] sm702-adapter_convert script - Sysmodtime Before: 1301382996000 2011-05-02 14:25:49,941 [http-8080-1] sm702-adapter_convert script - Error while calling function convert org.apache.bsf.BSFException: JavaScript Error: java.text.ParseException: Unparseable date: "[object JavaObject]"

Preston
  • 3,273
  • 4
  • 26
  • 35
  • 1
    What's your environment? I see strange mixture of Java and JavaScript here. Also, how do you want to use your converted `Date`, are you going to format it and display to user? – Paweł Dyda May 02 '11 at 22:23

2 Answers2

0

Presuming by "epoch" you mean a UNIX timestamp (seconds since the UNIX epoch), this time format is timezone-agnostic. That is, your timestamp is not UTC, GMT, CST or any other timezone. It's just a number of seconds.

You apply timezones when you re-format the timestamp as something human-readable. In this case, just load it into a Date object, and your local timezone should be used.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

For starters, toLocaleString() is Locale dependent and might be different than the format you have used (it is actually beyond your control).

Sencondly, if you know the CST time zone offset, you can always use getTime() add the offset and use setTime() to get this converted.
In Java world there is something like java.util.TimeZone that can give you right offset (use getOffset()) in milliseconds already.

Paweł Dyda
  • 18,366
  • 7
  • 57
  • 79