Question

Since the Date class in Java was deprecated, I had quite less frequently been using it but when I used it, felt that it might sometimes irritate someone as it displays the current year after subtracting 1900 from it. What should the designers have actually felt and imposed such a mechanism while the Calendar class and it's subclass GregorianCalendar return the exact year that is being maintained by the system as follows?

public class Main
{
    public static void main(String[] args)
    {
        Date d=new Date();
        System.out.println("Day : "+d.getDate()+" Month : "+d.getMonth()
        +" Year :  "+d.getYear());

        Calendar c=Calendar.getInstance();
        System.out.println("Day : "+c.get(Calendar.DATE)+" Month :  
       "+c.get(Calendar.MONTH)+" Year : "+c.get(Calendar.YEAR));
    }
}

It displays the output something like the one shown below, the current day, month and year.

Day : 1 Month : 10 Year : 111

Day : 1 Month : 10 Year : 2011


What the designers might have though about displaying 111 rather than 2011?

Was it helpful?

Solution

The subtraction is just for the sort year field 'y'. If you specify the long year field, 'Y' no subtraction occurs.

If you mean, why does the default toString use the short year format, I assume it was the most natural expression the java devs came up with for the average situation. If you need to display a Date object with a different formatted date, consider using the SimpleDateFormatter class: http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
formatter.format(new Date());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top