0

I don't know why I can't convert a String to a Date in Java Android. I got error when I try

The error :

W/System.err: java.text.ParseException: Unparseable date: "Fri Apr 30 00:12:13 GMT+02:00 2021"

My code :

String datestr = cursor.getString(cursor.getColumnIndex(UPDATED_AT)); // Fri Apr 30 00:12:13 GMT+02:00 2021
DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.GERMANY);
myDate = dateFormat.parse(datestr);

Edit:

I'm up to date now (I think):

I convert all my Date to

OffsetDateTime currentDate = OffsetDateTime.now()

That gives me :

2021-04-30T02:14:49.067+02:00

Then If this date is a String and I want to convert it to OffsetDateTime :

String datestr = cursor.getString(cursor.getColumnIndex(UPDATED_AT)); // 2021-04-30T02:14:49.067+02:00
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").withLocale( Locale.US );
OffsetDateTime myDate = OffsetDateTime.parse( datestr , f );
1
  • 1
    I recommend you don’t use SimpleDateFormat and Date. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use OffsetDateTime or ZonedDateTime and DateTimeFormatter, all from java.time, the modern Java date and time API. Commented Apr 30, 2021 at 9:05

1 Answer 1

1

tl;dr

OffsetDateTime
.parse( 
    "Fri Apr 30 00:12:13 GMT+02:00 2021" , 
    DateTimeFormatter
    .ofPattern( "EEE MMM dd HH:mm:ss OOOO uuuu" )
    .withLocale( Locale.US ) 
)
.toString()

2021-04-30T00:12:13+02:00

Avoid legacy date-time classes

You are using terrible date-time classes that were supplanted years ago by the modern java.time classes defined in JSR 310.

DateTimeFormatter

Define a formatting pattern to match your input text. Use DateTimeFormatter class.

Note the Locale, to determine the human language and cultural norms to use in translating the name of day & month, capitalization, abbreviation, and so on.

String input = "Fri Apr 30 00:12:13 GMT+02:00 2021";
DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEE MMM dd HH:mm:ss OOOO uuuu" ).withLocale( Locale.US );

OffsetDateTime

Your input represents a moment, a point on the timeline, as seen in the wall-clock time of an offset-from-UTC but not a time zone. Therefore, parse as a OffsetDateTime object.

OffsetDateTime odt = OffsetDateTime.parse( input , f );

odt.toString() = 2021-04-30T00:12:13+02:00


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. Hibernate 5 & JPA 2.2 support java.time.

Where to obtain the java.time classes?

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.