-1

I can't print GregorianCalendar

    package Tests;

import java.util.Date;
import java.util.GregorianCalendar;

public class Data 
{

    public static void main(String[] args) 
    {
        GregorianCalendar time = new GregorianCalendar(1999, 2, 2);
        System.out.println(time);
    }

}

I receive this

java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=false,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/Belgrade",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=119,lastRule=java.util.SimpleTimeZone[id=Europe/Belgrade,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=?,YEAR=1999,MONTH=2,WEEK_OF_YEAR=?,WEEK_OF_MONTH=?,DAY_OF_MONTH=2,DAY_OF_YEAR=?,DAY_OF_WEEK=?,DAY_OF_WEEK_IN_MONTH=?,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=?,ZONE_OFFSET=?,DST_OFFSET=?]

How I can print it?

user2540406
  • 79
  • 1
  • 3
  • 9
  • check SimpleDateFormat (see https://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html) – Leo Oct 30 '17 at 22:37

3 Answers3

0

you can use this like :-

GregorianCalendar calendar = new GregorianCalendar();
Date now = calendar.getTime();
System.out.println(now);

or

Calendar cal2 = new GregorianCalendar(2010, 9, 26);  // allocate with the specified date
cal2.get(Calendar.DAY_OF_WEEK);                      // 1 (Sunday) to 7 (Saturday)
Anshul Sharma
  • 3,432
  • 1
  • 12
  • 17
0

java.time

The modern solution uses the LocalDate class instead. Avoid GregorianCalendar as it is now obsolete.

LocalDate ld = LocalDate.of( 1999 , Month.FEBRUARY , 2 ) ;

ld.toString(): 1999-02-02

The toString method uses the standard ISO 8601 formats. For other formats, use the DateTimeFormatter class. Search Stack Overflow for more examples and discussion, as that has beeen covered many many times.

Generally best to let DateTimeFormatter automatically localize for you.

Locale locale = Locale.CANADA_FRENCH ;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL ).withLocale( locale ) ;
String output = ld.format( f ) ;

mardi 2 février 1999


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

Table of which java.time library to use with which version of Java or Android

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

when you call

System.out.println(time)

you've actually calling the toString() method of the Calendar instance (in this case, a GregorianCalendar one)

In order to have a properly formatted date, use SimpleDateFormat as stated in the Javadoc

https://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html

Leo
  • 751
  • 4
  • 29