Parsing Date from Calendar in Java

I have the following function

public static Date parseDate(String date, String format) throws ParseException { SimpleDateFormat formatter = new SimpleDateFormat(format); return formatter.parse(date); } 

I use this as follows in my code

 Calendar eDate = Calendar.getInstance(); eDate.add(Calendar.DAY_OF_MONTH,10); Date date = null; try { date = parseDate(eDate.getTime().toString(),"yyyy-MM-dd hh-mm-ss"); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } 

But he throws -

  java.text.ParseException: Unparseable date 

What is the problem?

+4
source share
8 answers

The format is not saved in Date . It is stored in String . Date#toString() returns a fixed format, which is described in Javadoc .

Make formatting only at the moment when you need to display Date for the person as String .

 Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DAY_OF_MONTH, 10); Date date = calendar.getTime(); String formattedDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date); System.out.println(formattedDate); 

Note that MM stands for months and MM for a few minutes. See Also SimpleDateFormat javadoc .

+11
source

You'll be glad to hear that you never need to parse a date from a Calendar object: a way to infer Date from Calendar via the getTime() method.


EDIT:

To display the date in eDate in ISO format:

 final DateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"); String formattedDate = isoFormat.format(eDate.getTime()); 

This is not verified, but I think it should work.

+5
source

You are currently formatting the default format from java.util.Date , and then dealing with a potentially different format. You should also change the format string - it currently uses a 12-hour clock without the am / pm indicator and twice the minutes. I think you mean: "yyyy-MM-dd HH-mm-ss"

+1
source

You insert a Zulu timestamp (UNIX), getTime() returns the number of milliseconds since January 1, 1970, 00:00:00 GMT . Then you define the format as yyyy-mm-dd hh-mm-ss and try to parse the timestamp using this pattern. Which does not match.

You can use Date date = calendar.getTime(); and then format it using new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(date);

+1
source

Do not use toString() for anything like this. toString() should only be used for debug messages.

Use DateFormat.format(..) to create a string in a predictable form.

+1
source

you can simply use the date returned by the calendar, instead of converting it to a string and back to a date (apparently using the wrong date format). Date can be obtained:

 eDate.getTime() 

There seems to be no need for SimpleDateFormat in your case.

0
source

Check the Date.toString () method.

Api claims that he returns it in the format:

 dow mon dd hh:mm:ss zzz yyyy 

which the:

 Mon Jan 28 14:22:07 EST 2004 

You tell the parser to expect: 2004-01-28 14-22-07

0
source
 eDate.getTime().toString() 

returns a string representation of the date in this format:

dow mon dd hh: mm: ss zzz yyyy (see API java.util.Date).

You are trying to parse a date in this format:

yyyy-mm-dd hh-mm-ss.

The code throws a ParseException correctly.

0
source

Source: https://habr.com/ru/post/1315662/


All Articles