1

I have two date format one in string "yyyy-MM-dd't'HH:mm:ss.SSSZ" and second one in long "yyyyMMddHHmmssSSS". I want to convert it into this format "yyyy-MM-dd HH:mm:ss", its convert successfully but when I print date.getTime() than

for first -> 1416490009109

for second -> 20141120121211800

code:

private DateTimeFormatter formater,formater1;
private SimpleDateFormat formate,formate1,formate2;

String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
String pattern1 = "yyyy-MM-dd HH:mm:ssZ";
//String pattern = "yyyy-MM-dd HH:mm:ss";
formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
formate1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
formate2 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
formater = DateTimeFormat.forPattern(pattern);
formater1 = DateTimeFormat.forPattern(pattern1);

String dateTime = "2011-11-20'T'11:42:12.672+00:00";
long savePoint = 20141120114212672L;

try {
    datetime = formater.parseDateTime(dateTime);
    date = datetime.toDate();
    //Log.d("Android: ", "Date  formate: "+datetime);
    dt = formate.format(date.getTime());
    Log.d("Android: ", "date Keys1:   "+date.getTime());
} catch (Exception e) {
    Log.d("Android: ", "Error: "+e);
}

for second one

try {
    String[] tzone = dateTime.split("[+]");
    String times = formate1.format(Long.parseLong(savePoint));
    datetime = formater.parseDateTime(times+"+"+tzone[tzone.length-1]);
    date = datetime.toDate();
    dt = formate.format(date.getTime());
} catch (Exception e) {
    Log.d("Android:", "Error:  "+e);
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Rishi Dwivedi
  • 908
  • 5
  • 19
  • Can you please provide code! what you have done so far? – atish shimpi Nov 21 '14 at 05:53
  • what exactly are you trying to achieve if conversion was successful? and what is wrong exactly? be specific! – Joseph118 Nov 21 '14 at 05:54
  • Have a look at [this question](http://stackoverflow.com/questions/12575990/calendar-date-to-yyyy-mm-dd-format-in-java/12576219#12576219), *"A Java Date is a container for the number of milliseconds since January 1, 1970, 00:00:00 GMT"* it has no concept for format. Use a `DateFormat` to display it in what ever format you want... – MadProgrammer Nov 21 '14 at 05:57
  • I am already using it but same date give different timestamp why – Rishi Dwivedi Nov 21 '14 at 06:01
  • `dt = formate.format(date.getTime());` can be `dt = formate.format(date);`. Using `date.getTime()` simply prints out it's number of milliseconds since the Unix epoch, try using `dt` instead... – MadProgrammer Nov 21 '14 at 06:08
  • actualy my purpose is to sorting with date so i am using date.getTime() – Rishi Dwivedi Nov 21 '14 at 06:09
  • why do you have a "T" between date and time???? – Joseph118 Nov 21 '14 at 06:21
  • 1
    @Joseph118 That `T` in the middle is part of one of the several useful date-time string formats defined by the [ISO 8601](http://en.m.wikipedia.org/wiki/ISO_8601) standard. That standard is used as a default by both the Joda-Time library and java.time package for parsing and generating strings. – Basil Bourque Nov 21 '14 at 06:54

2 Answers2

0

Count from epoch

The first input appears to be a count of milliseconds from the epoch reference of first moment of 1970 in UTC.

Parse the string into a long using Integer.parseLong.

long count = Long.parseLong( "1416490009109" ) ;
Instant instant = Instant.ofEpochMilli( count ) ; 

For more flexibility such as generating text in various formats, convert to OffsetDateTime or ZonedDateTime.

OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) :

String as integer

Your second input appears to be a string of year, month, day, hour, minute, second, fractional second masquerading as a long integer.

String input = "20141120121211800" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuuMMddHHmmssSSS" ) ;
LocalDateTime ldt = LocalDateTime.parse( input , f ) ;

Assign a time zone or offset-from-UTC if known with certainty.

OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ;

Generating strings

I want to convert it into this format "yyyy-MM-dd HH:mm:ss"

To produce text from your date-time object in your desired format, use the DateTimeFormatter class.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd HH:mm:ss" ) ;
String output = odt.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.

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

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

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

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

Use DateFormat to convert dates. This can help you out!

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date convertedDate = dateFormat.parse(stringDate);

If you want to compare dates later on use

date1.after(date2)

or

date1.before(date2)

more about date can be found on https://docs.oracle.com/javase/7/docs/api/java/util/Date.html

Joseph118
  • 505
  • 6
  • 21