格里高利历java错误

时间:2017-05-06 17:30:28

标签: java gregorian-calendar

我无法打印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);
    }

}

我收到了这个

  

java.util.GregorianCalendar中[时间= ?, areFieldsSet =假,areAllFieldsSet =假,宽大=真,区= sun.util.calendar.ZoneInfo [ID = “欧洲/贝尔格莱德”,偏移= 3600000,dstSavings = 3600000 ,useDaylight =真,过渡= 119,lastRule = java.util.SimpleTimeZone中[ID =欧洲/贝尔格莱德,偏移= 3600000,dstSavings = 3600000,useDaylight =真,startYear = 0,STARTMODE = 2,startMonth = 2,朝九特派= - 1,startDayOfWeek = 1,开始时间= 3600000,startTimeMode = 2,endMode = 2,endMonth = 9,endday指定= -1,一个endDayOfWeek = 1,结束时间= 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,微差= ?, ZONE_OFFSET = ?, DST_OFFSET =?]

我如何打印?

3 个答案:

答案 0 :(得分:0)

您可以使用它: -

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

Calendar cal2 = new GregorianCalendar(2010, 9, 26);  // allocate with the specified date
cal2.get(Calendar.DAY_OF_WEEK);                      // 1 (Sunday) to 7 (Saturday)

答案 1 :(得分:0)

java.time

现代解决方案使用LocalDate类。避免使用GregorianCalendar,因为它已经过时了。

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

ld.toString():1999-02-02

toString方法使用标准ISO 8601格式。对于其他格式,请使用DateTimeFormatter类。搜索Stack Overflow以获取更多示例和讨论,因为它已经多次覆盖。

通常最好让DateTimeFormatter自动为您定位。

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

mardi2février1999

关于java.time

java.time框架内置于Java 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如java.util.DateCalendar和& SimpleDateFormat

现在位于Joda-Timemaintenance mode项目建议迁移到java.time类。

要了解详情,请参阅Oracle Tutorial。并搜索Stack Overflow以获取许多示例和解释。规范是JSR 310

从哪里获取java.time类?

ThreeTen-Extra项目使用其他类扩展java.time。该项目是未来可能添加到java.time的试验场。您可以在此处找到一些有用的课程,例如IntervalYearWeekYearQuartermore

答案 2 :(得分:0)

致电

input =  aaxxYYxxaa
match = YY
requested_output = xxYYxx

您实际上正在调用Calendar实例的toString()方法(在本例中为GregorianCalendar)(

为了获得格式正确的日期,请使用Javadoc中所述的 SimpleDateFormat

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