Tuesday

Why is SimpleDateFormat changing given date?

//The upper case Y is "week year". It has 364 or 371 days 
//The lower case y has usual 365 or 366.
//So they give different outputs:
//Locale.forLanguageTag("tr_TR") >> comes with Java 7

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


public class Test {

    public static void main(String[] args) {
       try {
           String dateString = new java.util.Date().toString();
           System.out.println(dateString);

           SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.forLanguageTag("tr_TR"));
           Date date = format.parse(dateString);
           System.out.println(date.toString());

           SimpleDateFormat format2 = new SimpleDateFormat("EEE MMM dd HH:mm:ss z YYYY", Locale.forLanguageTag("tr_TR"));
           Date date2 = format2.parse(dateString);
           System.out.println(date2.toString());

       } catch (ParseException e) {
           //log
       }
    }
    
}
Output:

Mon May 14 10:31:14 EEST 2018
Mon May 14 10:31:14 EEST 2018
Mon Jan 01 09:31:14 EET 2018

No comments:

Post a Comment