How to convert String to Date using SimpleDateFormat?

I have this piece of code:

DateFormat formatter1; formatter1 = new SimpleDateFormat("mm/DD/yyyy"); System.out.println((Date)formatter1.parse("08/16/2011")); 

When I run this, I get this as output:

 Sun Jan 16 00:10:00 IST 2011 

I expected:

 Tue Aug 16 "Whatever Time" IST 2011 

I want to say that I do not expect a month. What mistake?

+55
java datetime date-format simpledateformat
Mar 26 2018-12-12T00:
source share
11 answers

Try the following:

 new SimpleDateFormat("MM/dd/yyyy") 
  • MM - "month" (not MM )
  • dd - "day" (not dd )

All this in javadoc for SimpleDateFormat


FYI, the reason your format is still a valid date format is because of the following:

  • MM - "minutes"
  • dd - "day of the year"

Also, you don't need the cast in Date ... it is already Date (or it explodes):

 public static void main(String[] args) throws ParseException { System.out.println(new SimpleDateFormat("MM/dd/yyyy").parse("08/16/2011")); } 

Output:

 Tue Aug 16 00:00:00 EST 2011 

Voila!

+85
Mar 26 '12 at 12:50
source share

m - min M - Months

 Letter Date or Time Component Presentation Examples G Era designator Text AD y Year Year 1996; 96 M Month in year Month July; Jul; 07 w Week in year Number 27 W Week in month Number 2 D Day in year Number 189 d Day in month Number 10 F Day of week in month Number 2 E Day in week Text Tuesday; Tue a Am/pm marker Text PM H Hour in day (0-23) Number 0 k Hour in day (1-24) Number 24 K Hour in am/pm (0-11) Number 0 h Hour in am/pm (1-12) Number 12 m Minute in hour Number 30 s Second in minute Number 55 S Millisecond Number 978 z Time zone General time zone Pacific Standard Time; PST; GMT-08:00 Z Time zone RFC 822 time zone -0800 
+38
Dec 03 '13 at 9:56
source share

Use the following function

 /** * Format a time from a given format to given target format * * @param inputFormat * @param inputTimeStamp * @param outputFormat * @return * @throws ParseException */ private static String TimeStampConverter(final String inputFormat, String inputTimeStamp, final String outputFormat) throws ParseException { return new SimpleDateFormat(outputFormat).format(new SimpleDateFormat( inputFormat).parse(inputTimeStamp)); } 

Usage example:

  try { String inputTimeStamp = "08/16/2011"; final String inputFormat = "MM/dd/yyyy"; final String outputFormat = "EEE MMM dd HH:mm:ss z yyyy"; System.out.println(TimeStampConverter(inputFormat, inputTimeStamp, outputFormat)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } 
+11
Feb 05 '13 at 10:02
source share
 String newstr = "08/16/2011"; SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy"); SimpleDateFormat format = new SimpleDateFormat("EE MMM dd hh:mm:ss z yyyy"); Calendar c = Calendar.getInstance(); c.setTime(format1.parse(newstr)); System.out.println(format.format(c.getTime())); 
+6
Mar 26 2018-12-12T00:
source share

A very simple example.

 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy"); Date date = new Date(); Date date1 = new Date(); try { System.out.println("Date1: "+date1); System.out.println("date" + date); date = simpleDateFormat.parse("01-01-2013"); date1 = simpleDateFormat.parse("06-15-2013"); System.out.println("Date1 is:"+date1); System.out.println("date" + date); } catch (Exception e) { System.out.println(e.getMessage()); } 
+4
Nov 11 '13 at 7:27
source share

Using

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

Instead of <

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

Insofar as,

 mm - is the key to minutes DD - is the key to day in Year 
+3
Jun 03 '15 at
source share

you can solve the problem very simply, how to first convert this string to a date object for example:

 java.util.Date date1 = new Date("11/19/2015"); SimpleDateFormat formatter = new SimpleDateFormat("MMM dd yyyy HH:mma"); String format = formatter.format(date); System.out.println(format); 
+3
Nov 19 '15 at 7:48
source share

This piece of code helps convert back and forth

  System.out.println("Date: "+ String.valueOf(new Date())); SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String stringdate = dt.format(new Date()); System.out.println("String.valueOf(date): "+stringdate); try { Date date = dt.parse(stringdate); System.out.println("parse date: "+ String.valueOf(date)); } catch (ParseException e) { e.printStackTrace(); } 
+3
Dec 24 '15 at 7:03
source share
  DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("MM/dd/uuuu"); System.out.println(LocalDate.parse("08/16/2011", dateFormatter)); 

Output:

2011-08-16

I contribute to the modern answer. The answer in Czech is correct and was a good answer when it was written 6 years ago. Now the infamous SimpleDateFormat class, SimpleDateFormat has long been deprecated, and we are much better at java.time , the modern Java date and time API. I highly recommend you use this instead of the old time classes.

What went wrong in the code?

When I parse on 08/16/2011 using your snippet, I get Sun Jan 16 00:08:00 CET 2011 . Since the lowercase mm is in a few minutes, I get 00:08:00 (8 minutes before midnight), and since the capital letter DD for the day of the year, I get January 16th.

In java.time too, the formatting pattern strings are case sensitive, and we need to use uppercase MM for the month and lowercase dd for the day of the month.

Question: Can I use java.time with my version of Java?

Yes, java.time works great on Java 6 and later and on older and newer Android devices.

  • In Java 8 and later and on new Android devices (from API level 26, as I said), a modern API is built-in.
  • In Java 6 and 7, get ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310, see the links below).
  • On (older) Android, the version of Android ThreeTen Backport is used. It is called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with org.threeten.bp .

communication

+2
Apr 07 '18 at 11:14
source share
 String localFormat = android.text.format.DateFormat.getBestDateTimePattern(Locale.getDefault(), "EEEE MMMM d"); return new SimpleDateFormat(localFormat, Locale.getDefault()).format(localMidnight); 

will return a format based on the device language. Note that getBestDateTimePattern () returns "the best possible localized form of this skeleton for a given locale"

+1
Mar 08 '18 at 22:25
source share

You used some type errors . If you want to install on 08/16/2011 in the following template. This is wrong because,

mm means minutes , use MM as for months

DD is wrong , it must be dd, which represents Days

Try this to achieve the result you want ( Tue Aug 16 "Whatever Time" IST 2011 ),

  String date = "08/16/2011"; //input date as String SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy"); // date pattern Date myDate = simpleDateFormat.parse(date); // returns date object System.out.println(myDate); //outputs: Tue Aug 16 00:00:00 IST 2011 
+1
Jul 25. '18 at 5:25
source share



All Articles