gregorian calendar java error
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?
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)
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
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
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
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.
上一篇: 使用date.tocalender函数添加日期字符串
下一篇: 公历日历java错误