0 votes

How to take expiration String from a Cookie to Date?


My problem is that I need to be able to carry a String which contains the expire date of a Cookie to the class Date then having the expire date in type Date using it to create an object of the class Cookie .

Previously from the class Date I would have been able to achieve it, but this method is currently discarded.

String dateExp = "Fri Jul 08 14:58:01 CEST 2022";
Date date = new Date(dateExp);

And I can't find the current way to be able to carry this String a Date using a Date Format . This is the code I used but it rejects it.

String dateExp = "Fri Jul 08 14:58:01 CEST 2022";
SimpleDateFormat objSDF = new SimpleDateFormat();    
Date date = objSDF.parse(dateExp);

Thank you and I look forward to your support.


Adrian Zubieta Points 11

The exception that shows you is ParseException, because SimpleDateFormat does not know in which format is the string that you are passing it, in the constructor of SimpleDateFormat receives as first parameter a String (pattern) so you can pass it the date format that you are trying to convert, but in your case you also need to pass it the second parameter that is of type Locale.

Here is an example:

String dateExp = "Fri Jul 08 14:58:01 CEST 2022";

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

Date date5 = formatter5.parse(dateExp);


I hope you find it useful. Best regards!


HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by: