简体   繁体   中英

Java date displays previous day , month and totally different year

I am trying to display Date based on timezone.

If I change my system time zone to US pacific time zone, today's date is displayed correctly. If I want to display 2000-01-01 output shows as 12/31/1969.

Can you please let me know if I have to make any change in system settings or java settings?. Below is the example code:

package timezoneexample;  
import java.text.DateFormat;
import java.util.Date;
import java.util.TimeZone;


public class TimezoneExample {

public static void main(String args[]) {
    DateFormat dateFormat = null;

    String datePattern = null;
    char dateSeperator = '/';
    try {
        datePattern = "MM/dd/yyyy";
        if (datePattern.length() <= 0)
            throw new java.util.MissingResourceException(
                    "Didn't find date format", "", "");
        boolean hasSeperatorAlready = false;
        for (int i = 0; i < datePattern.length(); i++)
            if (!Character.isLetter(datePattern.charAt(i)))
                if (hasSeperatorAlready)
                    throw new java.util.MissingResourceException(
                            "Unvalid date format", "", "");
                else
                    dateSeperator = datePattern.charAt(i);
    } catch (java.util.MissingResourceException mre) {
        System.out.println(mre);
    }

    dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
    if (datePattern.length() > 0
            && dateFormat instanceof java.text.SimpleDateFormat) {
        java.text.SimpleDateFormat sdf = (java.text.SimpleDateFormat) dateFormat;
        sdf.applyPattern(datePattern);
    }

    dateFormat.setTimeZone(java.util.TimeZone.getDefault());

    // enter DOB
    Date dob = new Date(2000 - 01 - 01);
    Date today = new Date();
    String timeZone = System.getProperties().getProperty("user.timezone");

    TimeZone tZone = TimeZone.getTimeZone(timeZone);

    System.out.println("Timezone : " + tZone);

    dateFormat.setTimeZone(tZone);

    System.out.println("Date Of Birth : " + dateFormat.format(dob));
    System.out.println("Date in Displayed as per Timezone : "
            + dateFormat.format(today));

}

}

Output:

Timezone : sun.util.calendar.ZoneInfo[id="America/Los_Angeles",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=America/Los_Angeles,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]

Date Of Birth : 12/31/1969

Date in Displayed as per Timezone : 01/07/2015

Your error is here:

Date dob = new Date(2000 - 01 - 01);

This will be interpreted as:

Date dob = new Date(1998);

This will invoke the Date(long date) constructor, resulting in a date near 1970/01/01.

What you most probably want is:

Date dob = new Date(2000, 1, 1);

new Date(...) requires a long value, expressing the number of milliseconds since 1/1/1970. You're specifying 2000 - 1 - 1 . This is NOT "year 2000, month 1 and day 1", it is a numeric expression equal to 1998 milliseconds.

To create a date based on year/month/day, use a Calendar :

Calendar c = Calendar.getInstance();
c.set(y, m-1 /* 0-based */, d);   // e.g. c.set(2000, 0, 1); 
return c.getTime();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM