1

I stored a MySQL DATE (from a web service) in a SQLite TEXT.

I'm no expert when working with dates, so as I was searching, I found you parse the date string into a java.util.Date.

This were my results:

// Works like a charm    
new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy", Locale.ENGLISH).parse(strdate)

// Unparseable date exception
new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH).parse(strdate);

Could I get some directions on this please?

Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206

4 Answers4

2

You are instructing the date parser that your input strdate is in format dd-MMM-yyyy. But actually it is in EEE MMM dd kk:mm:ss z yyyy format.

+----------------------------+------------------------------+-------------------+
| date pattern you tried     | your strdate input           | pattern  | parse  |
|                            |                              | matched? | error? |
+----------------------------+------------------------------+----------+--------+
| EEE MMM dd kk:mm:ss z yyyy | Fri Apr 04 06:55:24 GMT 2014 | yes      | no     |
+----------------------------+------------------------------+----------+--------+
| dd-MMM-yyyy                | Fri Apr 04 06:55:24 GMT 2014 | no       | yes    |
+----------------------------+------------------------------+----------+--------+

If you are trying to re format the same date string for display purposes, then

  1. construct a date object first with its default format received
  2. apply new pattern to return a string form of the same date.

Example:

String strdate = "Fri Apr 04 06:55:24 GMT 2014";  
String currentPattern = "EEE MMM dd kk:mm:ss z yyyy";  
SimpleDateFormat sdf = new SimpleDateFormat( currentPattern, Locale.ENGLISH );

Date dt = sdf.parse( strdate ); // new date is constructed
System.out.println( dt );

String newPattern = "dd-MMM-yyyy";     // new pattern is defined  
sdf.applyPattern( newPattern );        // the same is applied
String new_strdate = sdf.format( dt ); // on the date object
System.out.println( new_strdate );     // resulting new string form of date

Output would be as below:

Fri Apr 04 06:55:24 GMT 2014
04-Apr-2014
Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82
  • Now I'm not getting an exception, but the `new_strdate` needs to be in a `Date` object, but when I do again `SimpleDateFormat`, it goes back to the previous format – Christopher Francisco Apr 04 '14 at 12:45
1

try this:

Then to print with your required format you need a second SimpleDateFormat: sample code

SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",
                                        Locale.ENGLISH);

Date parsedDate = sdf.parse(date);
SimpleDateFormat print = new SimpleDateFormat("MMM d, yyyy HH:mm:ss");
System.out.println(print.format(parsedDate));

Notes:

  1. you should include the locale as if your locale is not English, the day name might not be recognised.

  2. IST is ambiguous and can lead to problems so you should use the proper time zone name if possible in your input.

Java timezone - strange behavior with IST?

http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html#timezone

Community
  • 1
  • 1
jmail
  • 5,944
  • 3
  • 21
  • 35
1

You can parse by this way,

Android

    Date yourdate = format.parse(strdate);
    SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MMM-yyyy");
    String date = DATE_FORMAT.format(yourdate);
    System.out.println("dd-MMM-yyyy format : " + date);

Result

    dd-MMM-yyyy format : 04-Apr-2014
Jaykumar Patel
  • 26,836
  • 12
  • 74
  • 76
1

parse expects Date String in the format you have specified in SimpleDateFormat constructor. check the date stored in SQLite, it might be in "EEE MMM dd kk:mm:ss z yyyy" format

M.Faizal
  • 450
  • 2
  • 12