Java Calender類の使用総括例

5761 ワード

実際のプロジェクトの中で、私達はいつも時間に対する処理に関連して、例えばウェブサイトに上陸して、私達はウェブサイトのトップページがXXXを表示することを見て、あなたを歓迎します!今日はXXX年です。。。。いくつかのウェブサイトはユーザー登録の時間を記録しています。例えば銀行のウェブサイトで、これらの常に処理しなければならない問題に対して、JavaはCalendarという専門的な日付操作の種類を提供しています。この種類は何か特別なところがありますか?まず、Calendarの声明を見に来ました。

public abstract class Calendar extends Objectimplements Serializable, Cloneable, Comparable<Calendar>
このクラスはabstractによって修飾され、newによってインスタンスが得られないと説明されているが、Calendarは、このタイプの汎用的なオブジェクトを獲得するために、Calendarオブジェクト(対象はCalendarのサブクラスのペア)に戻り、カレンダフィールドは現在の日付と時間によって初期化されている。

Calendar rightNow = Calendar.getInstance();
なぜ戻ってきたのかというと、Calendarのサブクラスのオブジェクトは、国ごとに独自のカレンダーアルゴリズムがあります。たとえば、西洋諸国の最初の週はほとんど日曜日です。中国は月曜日です。

/**
 * Gets a calendar using the default time zone and locale. The
 * <code>Calendar</code> returned is based on the current time
 * in the default time zone with the default locale.
 *
 * @return a Calendar.
 */
public static Calendar getInstance()
{
  Calendar cal = createCalendar(TimeZone.getDefaultRef(), Locale.getDefault(Locale.Category.FORMAT));
  cal.sharedZone = true;
  return cal;
}
このうちcreateCalender方法は、国によっては日付を返します。

private static Calendar createCalendar(TimeZone zone,
                      Locale aLocale)
  {
    Calendar cal = null;

    String caltype = aLocale.getUnicodeLocaleType("ca");
    if (caltype == null) {
      // Calendar type is not specified.
      // If the specified locale is a Thai locale,
      // returns a BuddhistCalendar instance.
      if ("th".equals(aLocale.getLanguage())
          && ("TH".equals(aLocale.getCountry()))) {
        cal = new BuddhistCalendar(zone, aLocale);
      } else {
        cal = new GregorianCalendar(zone, aLocale);
      }
    } else if (caltype.equals("japanese")) {
      cal = new JapaneseImperialCalendar(zone, aLocale);
    } else if (caltype.equals("buddhist")) {
      cal = new BuddhistCalendar(zone, aLocale);
    } else {
      // Unsupported calendar type.
      // Use Gregorian calendar as a fallback.
      cal = new GregorianCalendar(zone, aLocale);
    }

    return cal;
  }
日付操作をより便利にするために、Calendar類はYEAR、MONTH、DAY_OF_MONTH、HOURなどのカレンダフィールド間の変換にはいくつかの方法が提供され、カレンダフィールドを操作する(例えば来週の日付を取得する)方法が提供される。瞬間的にはミリ秒の値で表示されます。これはグレイ暦からのオフセットです。
次にCalendarのよく使う方法を見てみます。

package com.test.calendar;

import java.util.Calendar;

import org.junit.Before;
import org.junit.Test;

public class CalendarDemo {
  Calendar calendar = null;

  @Before
  public void test() {
    calendar = Calendar.getInstance();
  }

  //     ,          
  @Test
  public void test1() {
    //    
    int year = calendar.get(Calendar.YEAR);

    //    ,            0~11,           +1       
    int month = calendar.get(Calendar.MONTH) + 1;

    //    
    int day = calendar.get(Calendar.DAY_OF_MONTH);

    //    
    int hour = calendar.get(Calendar.HOUR);
    // int hour = calendar.get(Calendar.HOUR_OF_DAY); // 24    

    //    
    int minute = calendar.get(Calendar.MINUTE);

    //    
    int second = calendar.get(Calendar.SECOND);

    //   ,              
    int weekday = calendar.get(Calendar.DAY_OF_WEEK);

    System.out.println("   " + year + " " + month + " " + day + " " + hour
        + " " + minute + " " + second + " " + "  " + weekday);
  }

  //       
  @Test
  public void test2() {
    //           calendar.add(Calendar.MONTH, 1);
    calendar.add(Calendar.YEAR, 1);

    //    
    int year = calendar.get(Calendar.YEAR);

    //    
    int month = calendar.get(Calendar.MONTH) + 1;

    //    
    int day = calendar.get(Calendar.DAY_OF_MONTH);

    System.out.println("      :" + year + " " + month + " " + day + " ");
  }

  //             
  @Test
  public void test3() {
    //    6      
    int currentMonth = 6;
    //    7      ,     6        currentMonth  
    // 1
    calendar.set(calendar.get(Calendar.YEAR), currentMonth, 1);

    calendar.add(Calendar.DATE, -1);

    //    
    int day = calendar.get(Calendar.DAY_OF_MONTH);

    System.out.println("6        " + day + " ");
  }

  //     
  @Test
  public void test4() {
    calendar.set(Calendar.YEAR, 2000);
    System.out.println("   " + calendar.get(Calendar.YEAR) + " ");

    calendar.set(2008, 8, 8);
    //    
    int year = calendar.get(Calendar.YEAR);

    //    
    int month = calendar.get(Calendar.MONTH);

    //    
    int day = calendar.get(Calendar.DAY_OF_MONTH);

    System.out.println("   " + year + " " + month + " " + day + " ");
  }
}
プログラム出力結果:
1今は2016年11月7日11時42分18秒火曜日です。
2年後の今日:2017年11月7日
3月の最後の日は30日です。
4今は2000年です
5今は2008年8月8日です。
Calendar類にもbefore、after、compreToなどの方法があります。使い方はDate類と似ていますが、現在はCalendar類の操作日付を推奨しています。
以上は小编が绍介したJava Calendar类の使用総括の実例です。详しく整理して、皆さんに助けてほしいです。もし何か疑问があれば、メッセージをください。小编はすぐに返事します。ここでも私たちのサイトを応援してくれてありがとうございます。