简体   繁体   English

Java公历日历输出错误的日期

[英]Java Gregorian calendar outputting wrong date

im working on a Gregorian calendar project where i have to print 100 days from today and day of week of my birthday. 我正在做一个格里高利历计划,我必须从今天和生日的第几天开始打印100天。 The program displays a day, however its the wrong day. 程序显示一天,但是显示错误的一天。 could you guys help me with the problem? 你们能帮我解决这个问题吗? thanks! 谢谢!

import java.util.GregorianCalendar;
public class Gregorian {

    public static void main(String[] args) 
    {
        Day today = new Day();
          System.out.print("Today: ");
          System.out.println(today.toString());

        GregorianCalendar Date = new GregorianCalendar();
        Date.add(GregorianCalendar.DAY_OF_MONTH, 100);
        CalendarUtils utils = new CalendarUtils();
        String day = utils.getWeekday(Date.get(GregorianCalendar.DAY_OF_WEEK));

        int year=Date.get(GregorianCalendar.YEAR);
        int month=Date.get(GregorianCalendar.MONTH);
        int dayof=Date.get(GregorianCalendar.DAY_OF_MONTH);

        System.out.println("100 days from today: " + year + "/" + month + "/" + dayof + " which is a: " + day);


        GregorianCalendar Birthday = new GregorianCalendar(2012,1,1);
        String Bday = utils.getWeekday(Birthday.get(GregorianCalendar.DAY_OF_WEEK ));
        System.out.println("Weekday of my Birthday: " + Bday );

        Birthday.add(GregorianCalendar.DAY_OF_MONTH, 10000);

        int Byear=Birthday.get(GregorianCalendar.YEAR);
        int Bmonth=Birthday.get(GregorianCalendar.MONTH);
        int Bdayof=Birthday.get(GregorianCalendar.DAY_OF_MONTH);

        System.out.println("10000 days from my Birthday: " + Byear + "/" + Bmonth + "/" + Bdayof);

Here is the CalendarUtils 这是CalendarUtils

import java.util.GregorianCalendar;
public class CalendarUtils
{
   /**
      Returns the String for GregorianCalendar DAY_OF_WEEK
   */
   public String getWeekday(int day_of_week)
   {
      String day = "";

      if (day_of_week == GregorianCalendar.SUNDAY)
      {
         day = "Sunday";
      }
      else if (day_of_week == GregorianCalendar.MONDAY)
      {
         day = "Monday";
      }
      else if (day_of_week == GregorianCalendar.TUESDAY)
      {
         day = "Tuesday";
      }
      else if (day_of_week == GregorianCalendar.WEDNESDAY)
      {
         day = "Wednesday";
      }
      else if (day_of_week == GregorianCalendar.THURSDAY)
      {
         day = "Thursday";
      }
      else if (day_of_week == GregorianCalendar.FRIDAY)
      {
         day = "Friday";
      }
      else if (day_of_week == GregorianCalendar.SATURDAY)
      {
         day = "Saturday";
      }

      return day;
   }

   /**
      Returns the string of GregorianCalendar MONTH
   */
   public String getMonth(int month)
   {
      String monthStr = "";

      if (month == GregorianCalendar.JANUARY)
      {
         monthStr = "January";
      }
      else if (month == GregorianCalendar.FEBRUARY)
      {
         monthStr = "February";
      }
      else if (month == GregorianCalendar.MARCH)
      {
         monthStr = "March";
      }
      else if (month == GregorianCalendar.APRIL)
      {
         monthStr = "April";
      }
      else if (month == GregorianCalendar.MAY)
      {
         monthStr = "May";
      }
      else if (month == GregorianCalendar.JUNE)
      {
         monthStr = "June";
      }
      else if (month == GregorianCalendar.JULY)
      {
         monthStr = "July";
      }
      else if (month == GregorianCalendar.AUGUST)
      {
         monthStr = "August";
      }
      else if (month == GregorianCalendar.SEPTEMBER)
      {
         monthStr = "September";
      }
      else if (month == GregorianCalendar.OCTOBER)
      {
         monthStr = "October";
      }
      else if (month == GregorianCalendar.NOVEMBER)
      {
         monthStr = "November";
      }
      else if (month == GregorianCalendar.DECEMBER)
      {
         monthStr = "December";
      }

      return monthStr;
   }
}

tl;dr tl; dr

LocalDate.now()          // Determine current date (no time-of-day) for the JVM’s current default time zone.
    .plusDays( 100 )       // Add days.
    .getDayOfWeek()     // Get `DayOfWeek` enum object.
    .getDisplayName( FormatStyle.FULL , Locale.ITALY )    // Generate a string of the name of this day-of-week automatically localized with a certain length/abbreviation. 

Details 细节

The troublesome Calendar class is now legacy, supplanted by the java.time classes. 麻烦的Calendar类现在已被遗留,由java.time类取代。

You want to add a hundred days after today. 您想在今天之后增加一百天。

LocalDate today = LocalDate.now();
LocalDate hundred = today.plusDays( 100 );

You want the day-of-week of your birthday this year. 您想要今年生日的星期几。

MonthDay birthday = MonthDay.of( Month.JANUARY , 23 );
LocalDate birthdayThisYear = birthday.atYear( today.getYear() );
DayOfWeek dow = birthdayThisYear.getDayOfWeek();

You want the name of month and name of day-of-week. 您需要月份的名称和星期几的名称。 Let java.time automatically localize for you. 让java.time自动为您本地化。

Locale locale = Locale.CANADA_FRENCH ;  // Or Locale.US, etc.
String m = today.getMonth().getDisplayName( TextStyle.FULL , locale );
String d = dow.getDisplayName( TextStyle.FULL , locale );

I suppose you want to enter the 1st of January 2012 as the birthday? 我想您要输入2012年1月1日作为生日吗? The the correct line would be: 正确的行是:

GregorianCalendar Birthday = new GregorianCalendar(2012, 0, 1);

or better: 或更好:

GregorianCalendar Birthday = new GregorianCalendar(2012, Calendar.JANUARY, 1);

The month is zero based (0 = January, 1 = February,...). 月份从零开始(0 =一月,1 =二月,...)。 And this results in the output: Weekday of my Birthday: Sunday 结果是: 我生日的工作日:周日

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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