47684

SimpleDateFormat incorrectly rolling year forward during format of Date [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/43933597/eee-mmm-dd-hhmmss-zzz-yyyy-date-format-to-java-sql-date" dir="ltr" rel="nofollow">“EEE MMM dd HH:mm:ss ZZZ yyyy” date format to java.sql.Date</a> <span class="question-originals-answer-count"> 1 answer </span> </li> </ul>

I am currently working with a DateUtils class, that helps format dates across a system. I have a Date in format yyyyMMdd, which is parsed from a String.

I pass into another format to receive a "period" value, of format E d MMM YY.

The below, should give me a period of MON 31 DEC 18. However it is returning MON 31 DEC 19. Why is the year rolling forward?

Thanks

final SimpleDateFormat dateFormat_E_d_MMM_YY = new SimpleDateFormat("E d MMM YY"); final SimpleDateFormat dateFormat_yyyyMMdd = new SimpleDateFormat("yyyyMMdd"); final Date date = dateFormat_yyyyMMdd.parse("20181231"); System.out.println(date); System.out.println(dateFormat_E_d_MMM_YY.format(date));

Answer1:

You have a typo use E d MMM yy instead of E d MMM YY.

From the docs :

<blockquote>

y : year

Y : Week year

</blockquote>

Answer2:

You have written YY which means week. You need the year, hence you will need to write yy

In case you need more details you can check all the meaning of the different variables in the documentation of <a href="https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html" rel="nofollow">SimpleDateFormat</a> class.

y Year Year 1996; 96 Y Week year Year 2009; 09 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

Recommend

  • How to read events for a particular date from native calendar
  • Android Pulling Data from database
  • TypeScript: Cannot read property 'transform' of undefined in datepipe.transform
  • How to remove millisec from date and time [duplicate]
  • Dates in .net vs in javascript
  • is it possible to have a new file for each new day with log4cXX
  • How to convert date to week number
  • Google Sheets API v4 and valueInputOption
  • Jsoup parsing an Html file with a tbody tag
  • R Error - cannot change value of locked binding for 'df'
  • Getting the “Do you want to overwrite the file” dialog box to show when saving with VBA
  • NSDateFormatter still parsing instead having incorrect format
  • x86: Count transitions from 1 to 0 in 32 bit number
  • Get Quarters StartDate and EndDate from Year
  • MS SQL Server 2008 :Getting start date and end date of the week to next 8 weeks
  • How can you tell if a Python program has anything to read from stdin?
  • Aggregating two data frame columns without any existing pattern logic
  • Grid creating extra spacing that I don't want
  • data.table replicate rows after join?
  • Getting IIS6 to play nice with WordPress Pretty Permalinks
  • Update varbinary(MAX) field in SQLServer 2012 Lost Last 4 bits
  • Parenthesis() and SQL Query Performance
  • rewrite uppercase url to lowercase url htaccess
  • Email verification using google app script and google forms
  • Linq Objects Group By & Sum
  • Optimizing database types to compact database (SQLite)
  • How to set my toolbar fixed while scrolling android
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • Buffer size for converting unsigned long to string
  • Java static initializers and reflection
  • How does Linux kernel interrupt the application?
  • apache spark aggregate function using min value
  • unknown Exception android
  • Sorting a 2D array using the second column C++
  • reshape alternating columns in less time and using less memory
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app