1

I want to convert milliSeconds in long format to Gregorian Calendar. By searching in the web, i use the code below:

public static String getStringDate(int julianDate){
    GregorianCalendar gCal = new GregorianCalendar();
    Time gTime = new Time();
    gTime.setJulianDay(julianDate);
    gCal.setTimeInMillis(gTime.toMillis(false));
    String gString = Utils.getdf().format(gCal.getTime());
    return gString;
}

public static SimpleDateFormat getdf(){
    return new SimpleDateFormat("yyyy-MM-dd, HH:MM",Locale.US);
}

Yes, the code works but i find that only the date and the hour are correct but there are errors on minutes. Say if the thing happens on 2014-11-06, 14:00, it will give me 2014-11-06, 14:11. I want to know are there any solutions to modify it or it is not recommended to convert time into Gregorian Calendar. Many thanks!

Antoine Murion
  • 773
  • 1
  • 14
  • 26

2 Answers2

0

The problem actually is very simple, modify SimpleDateFormat("yyyy-MM-dd, HH:MM",Locale.US) with

SimpleDateFormat("yyyy-MM-dd, HH:mm",Locale.getDefault());

will solve the problem

Antoine Murion
  • 773
  • 1
  • 14
  • 26
  • FYI, the troublesome old date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the [java.time](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes. Much of the java.time functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android in the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Feb 01 '18 at 05:30
0

tl;dr

Instant.ofEpochMilli( millis )                  // Convert count-from-epoch into a `Instant` object for a moment in UTC.
    .atZone( ZoneId.of( "Pacific/Auckland" ) )  // Adjust from UTC to a particular time zone. Same moment, different wall-clock time. Renders a `ZonedDateTime` object.
    .format(                                    // Generate a String in a particular format to represent the value of our `ZonedDateTime` object.
        DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd, HH:mm" )
    )

java.time

The modern approach uses the java.time classes instead of those troublesome legacy classes.

Convert your count of milliseconds since the epoch reference of first moment of 1970 (1970-01-01T00:00Z) to a Instant object. Be aware that Instant is capable of finer granularity of nanoseconds.

Instant instant = Instant.ofEpochMilli( millis ) ;

That moment is in UTC. To adjust into another time zone, apply a ZoneId to get a ZonedDateTime.

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment, so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;

Generate a string in your desired format using a DateTimeFormatter object.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd, HH:mm" , Locale.US ) ;
String output = zdt.format( f ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154