Mithun Sreedharan
Mithun Sreedharan

Reputation: 51292

Android SimpleDateFormat problem

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = (Date)formatter.parse("2011-09-13");
Log.e(MY_DEBUG_TAG, "Output is "+ date.getYear() + " /" + date.getMonth() + " / "+ (date.getDay()+1));

Is out putting

09-13 14:20:18.740: ERROR/GoldFishActivity(357): Output is 111 /8 / 3

What is the issue?

Upvotes: 4

Views: 2147

Answers (3)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79620

java.time

In March 2014, Java 8 introduced the modern, java.time date-time API which supplanted the error-prone legacy java.util date-time API. Any new code should use the java.time API*.

Solution using modern date-time API

Since the default formats used in the modern date-time API are based on ISO 8601 standards, you are not required to use a DateTimeFormatter object explicitly to parse a date string conforming to the ISO 8601 standards. You can directly use LocalDate#parse to parse it.

Formatted string for your logging:

You do not need to get individual date elements and join them to obtain a formatted string for your logging. You can create another DateTimeFormatter object with the required format and format the LocalDate to get the required string as shown below:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
Log.e(MY_DEBUG_TAG, String.format("Output is %s", date.format(formatter)));

Demo:

public class Main {
    public static void main(String args[]) {
        LocalDate date = LocalDate.parse("2011-09-13");
        System.out.println(date);

        // Formatted string for logging
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        System.out.println(String.format("Output is %s", date.format(formatter)));
    }
}

Output:

2011-09-13
Output is 2011/09/13

Online Demo

Note: For whatever reason, if you need an instance of java.util.Date from this object of LocalDate, you can do so as follows:

Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant());

Learn more about the modern date-time API from Trail: Date Time.


* If you receive a java.util.Date object, convert it to a java.time.Instant object using Date#toInstant, and derive other java.time date-time objects from it.

Upvotes: 3

dogbane
dogbane

Reputation: 274878

The methods you are using in the Date class are deprecated.

  • You get 111 for the year, because getYear() returns a value that is the result of subtracting 1900 from the year i.e. 2011 - 1900 = 111.
  • You get 3 for the day, because getDay() returns the day of the week and 3 = Wednesday. getDate() returns the day of the month, but this too is deprecated.

You should use the Calendar class instead.

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = (Date)formatter.parse("2011-09-13");        
Calendar cal = Calendar.getInstance();  
cal.setTime(date);
Log.e(MY_DEBUG_TAG, "Output is "+ cal.get(Calendar.YEAR)+ " /" + (cal.get(Calendar.MONTH)+1) + " / "+ cal.get(Calendar.DAY_OF_MONTH));

Upvotes: 6

JB Nizet
JB Nizet

Reputation: 692231

Read the javadoc of java.util.Date carefully.

getYear returns the number of years since 1900.

getMonth returns the month, starting from 0 (0 = January, 1 = February, etc.).

getDay returns the day of week (0 = Sunday, 1 = Monday, etc.), not the day of month.

And all these methods are deprecated. You shouldn't use them anymore.

Upvotes: 4

Related Questions