0

I'm trying to convert a String that represents a date stored in SQLITE.

The date was stored into sqlite as follows:

Date date;

date.toString();

According with Java documentation, toString() method:

Returns a string representation of this Date. The formatting is equivalent to using a SimpleDateFormat with the format string "EEE MMM dd HH:mm:ss zzz yyyy", which looks something like "Tue Jun 22 13:07:00 PDT 1999". The current default time zone and locale are used. If you need control over the time zone or locale, use SimpleDateFormat instead.

Until here, it's fine but, when I try to get the String and convert it to date again, Java throws an exception.

The String comes from sqlite:

Mon Jan 20 18:26:25 BRT 2014

So, I do:

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);

Date date= sdf.parse("Mon Jan 20 18:26:25 BRT 2014");

What I'm doing wrong?

Thanks.

blackpanther
  • 10,998
  • 11
  • 48
  • 78
vitorvigano
  • 697
  • 2
  • 8
  • 18

3 Answers3

1

try this code

String dateString = "here your date";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date convertedDate = new Date();
try {
    convertedDate = dateFormat.parse(dateString);
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
System.out.println(convertedDate);
mohammed momn
  • 3,230
  • 1
  • 20
  • 16
  • This answer seems to have it upside down: it inputs the output format and outputs the input format. – laalto Jan 20 '14 at 22:00
  • Sure. "Mon Jan 20 18:26:25 BRT 2014" – vitorvigano Jan 20 '14 at 22:07
  • i try with same format but without "BRT" it work with me , if you can remove BRT from your format ? – mohammed momn Jan 20 '14 at 22:57
  • FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Sep 03 '18 at 06:24
1

Try this:

String w = "Mon Jan 20 18:26:25 BRT 2014";
SimpleDateFormat pre = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try{
    Date date = pre.parse(w);
    System.out.println(sdf.format(date));
}catch(Exception e){
    e.printStackTrace();
}

Output:

20/01/2014
0

Formatter for storing and restoring data value in format dd/MM/yyyy

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");

Storing data

String dataAsString = simpleDateFormat.format(date); // 20/01/2014

Restoring data

Date data = simpleDateFormat.parse(dataAsString);
MariuszS
  • 30,646
  • 12
  • 114
  • 155