69209

Java DateFormat format and parse gives unexpected result

Question:

I'm getting unexpected output when running following code,

DateFormat df = new SimpleDateFormat("YYYY-MM-dd"); Date date = df.parse("2012-06-23"); System.out.println(df.format(date));

Output : 2012-01-01

I'm expecting 2012-06-23 as output,what am i doing wrong ?

Answer1:

yyyy should be all lowercase, try that and see if it makes a difference...

Answer2:

Refer this <a href="http://www.tutorialspoint.com/java/java_date_time.htm" rel="nofollow">Date & time in Java</a>. You'll get the date format used

Use 'y' instead of 'Y'.

DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date date = df.parse("2012-06-23"); System.out.println(df.format(date));

Recommend

  • Date time object from webservice showing on Android textview with pattern?
  • highlighting specific date in calendar view without onClickListener
  • Using JQuery Datepicker To Only Show Months
  • How to use java.time.LocalDate on a Play Framework JSON Rest?
  • Year, Month, and Day parameters describe an un-representable DateTime in Persian calender
  • java program to calculate XIRR without using excel or any other library
  • jQuery - how to validate a date of birth using jQuery Validation plugin?
  • jQuery random blockquote
  • A simple datepicker in VueJS
  • How to make Plotly chart with year mapped to line color and months on x-axis
  • How to parsing NSDate to RFC 822 always use in English?
  • iOS Date formatting
  • stop execution in Custom validator if it false
  • Connecting bluetooth device to windows phone 8 application
  • Get localized short date pattern as String?
  • Find Previous month name using Calender or any classes that in java
  • CXF JAXB JAXBEncoderDecoder unmarshalling error : unexpected element when having qualified elements
  • Unexpected token ILLEGAL while running node.js mocha test
  • Combining two different ActiveRecord collections into one
  • Stop Bash Script if Hive Fails
  • Parse a date string in a specific locale (not timezone!)
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • Why querying a date BC is changed to AD in Java?
  • Row Count Is Returning the incorrect number using RaptureXML
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Illegal mix of collations for operation for date/time comparison
  • Master page gives error
  • Deserializing XML into class C#
  • ActionScript 2 vs ActionScript 3 performance
  • Release, debug version and Authorization Google?
  • How can I estimate amount of memory left with calling System.gc()?
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Apache 2.4 - remove | delete | uninstall
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Hits per day in Google Big Query
  • python draw pie shapes with colour filled
  • Can't mass-assign protected attributes when import data from csv file
  • sending mail using smtp is too slow
  • How to Embed XSL into XML