0

I've a stream of date values that arrive to me in a numeric format that I assume it's a Julian format (not sure about it). I need to compare those dates with my own date, so I need to convert "my" date to that numeric format. I cant' do viceversa for performance issues.

Example. My date si in "classic" format:

"Wed Jan 21 10:21:57 CET 2015"

I need to convert it to this numeric format (is it Julian?):

"1421832117"

How? Pay attention: in the Julian date I need also time infos, not only date!

Thank you very much.

Geltrude
  • 1,093
  • 3
  • 17
  • 35
  • 1
    Maybe [this](http://stackoverflow.com/questions/14535983/convert-a-regular-date-to-julian-date-and-vice-versa-in-java) helps. – Jens Jan 29 '15 at 11:27
  • 1
    It doesn't look like a Julian date, I think it is the number of seconds since Unix epoch. – Andy Turner Jan 29 '15 at 11:31
  • Indeed - which is easy to get at once you've got a `Date` object. Where does "my" date come from? Is it really as a string, or is that just the result of calling `toString()` on a `Date`? – Jon Skeet Jan 29 '15 at 11:32
  • Well you can use Joda time library if you need further help or snippet for it you may ask here – Syed Atir Mohiuddin Jan 29 '15 at 11:44

2 Answers2

0

You may pass Arg (Input) with your provided date format and the method below will parse it in Millis (your desired format)

private void parseDateTime(String input){
    DateTimeZone CET = DateTimeZone.forID("CET");
    DateTimeFormatter dtf = DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss 'CET' yyyy").withZone(CET);
    DateTime parsed = dtf.parseDateTime(input);
    Log.d("Millis for classic","" + parsed.getMillis());
}

OUTPUT : 01-28 20:40:56.787: D/Millis for classic(22559): 1421832117000

0

Count of seconds since 1970

You have apparently mistaken a count of whole seconds since the epoch of first moment of 1970 UTC as a Julian day number. While both counts, these are entirely beasts.

A Julian day number is a count of whole or fractional days since January 1, 4713 BC, proleptic Julian calendar (November 24, 4714 BC, in the proleptic Gregorian calendar). The Julian day number for the date seen below, 2015-01-21 is 2457043. Very different from the 1421832117 input seen in the Question.

java.time

Using the java.time framework built into Java 8 and later, we can easily convert your count of seconds into a date-time object. See Oracle Tutorial to learn about java.time. Much of the java.time functionality has been back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

An Instant is a moment on the timeline in UTC with a resolution of up to nanoseconds.

Instant now = Instant.now();

We can easily convert your count of seconds since start of 1970 UTC into an Instant object.

long seconds = 1421832117L; // Count of whole seconds since start of 1970 UTC.
Instant instant = Instant.ofEpochSecond ( seconds );

You may want to view this by the wall-clock time of some time zone. Avoid using 3-4 letter abbreviations such as CET as these are not true time zones, not standardized, and are not even unique(!). A proper time zone name is in the format of continent/region.

ZoneId zoneId = ZoneId.of ( "Europe/Paris" );
ZonedDateTime zdt = ZonedDateTime.ofInstant ( instant , zoneId );

Dump to console.

System.out.println ( "seconds:" + seconds + " | instant: " + instant + " | zdt: " + zdt );

seconds:1421832117 | instant: 2015-01-21T09:21:57Z | zdt: 2015-01-21T10:21:57+01:00[Europe/Paris]

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