Javaカレンダークラスの時間操作は、おそらくカレンダーを作成して管理する最も簡単なソリューションです。デモコードは簡単で、時間の蓄積と減算、および日付と時刻の比較を示しています。
注:
カレンダーの月は0から始まります。つまり、年の12か月は0〜11で表されます。
カレンダーの定義と価値は次のとおりです。
カレンダー.sunday = 1
calendar.monday = 2
calendar.tuesday = 3
カレンダー.Wednesday = 4
カレンダー。thursday= 5
カレンダー。friday= 6
カレンダー.Saturday = 7
SimpledateFormatの形式定義
Javaカレンダーデモコードは次のとおりです。
パッケージデモ; Import Java.util.date; java.text.simpledateformatをインポートします。 java.text.dateformatをインポートします。 java.text.parseexceptionをインポートします。 java.util.calendarをインポートします。 public class test {public test(){} public static void main(string [] args){// String Conversion Date Format // dateFormat fmtdateTime = new SimpledateFormat( "yyyy-mm-dd hh:mm:ss"); //受信パラメーターを受信// string strdate = args [1]; //日付フォーマットオブジェクトを取得//日付= fmtdatetime.parse(strdate); //今日の日付時刻文字列str =(new SimpledateFormat( "yyyy-mm-dd hh:mm:ss:sss"))。形式(new date()))の完全な表示; System.out.println(str); //カレンダーオブジェクトカレンダーカレンダー= calendar.getInstance()を作成します。 {//カレンダーの時間を設定する方法//渡された時間形式を設定しますSimpledateFormat dateFormat = new SimpledateFormat( "yyyy-md h:m:s"); //日付を指定する日付= dateFormat.Parse( "2013-6-1 13:24:16"); //カレンダーを日付カレンダーに設定します。Settime(date); //特定の形式で設定するだけの時間を表示str =(new SimpledateFormat( "yyyy-mm-dd hh:mm:ss:sss"))。 System.out.println(str); } catch(parseexception e){e.printstacktrace(); } //別のカレンダーメソッド//年、月、日付、時間、分、分、2番目のカレンダー= calendar.getInstance()を除算します。 Calendar.set(2013、1、2、17、35、44); str =(new SimpledateFormat( "yyyy-mm-dd hh:mm:ss:sss"))。format(calendar.gettime()); System.out.println(str); //現在の時刻を取得するカレンダーメソッド//または日付を使用してカレンダーオブジェクトカレンダーを初期化します。Settime(new date()); // settimeは上記の行に似ています//日付= new date(); // calendar.settime(date); str =(new SimpledateFormat( "yyyy-mm-dd hh:mm:ss:sss"))。format(calendar.gettime()); System.out.println(str); // show year int year = calendar.get(calendar.year); system.out.println( "year is =" + string.valueof(year)); // show month(0から始まる、実際のディスプレイを追加する必要があります)int month = calendar.get(calendar.month); system.out.println( "nth is =" +(month + 1)); //週の何日int week = calendar.get(calendar.day_of_week); system.out.println( "week is =" + week); //今年のn日int day_of_year = calendar.get(calendar.day_of_year); System.out.println( "day_of_year is =" + day_of_year); //今月のn日int day_of_month = calendar.get(calendar.day_of_month); System.out.println( "day_of_month =" + string.valueof(day_of_month)); // 3時間後のcalendar.add(calendar.hour_of_day、3); int hour_of_day = calendar.get(calendar.hour_of_day); System.out.println( "hour_of_day + 3 =" + hour_of_day); //現在の時間int minute = calendar.get(calendar.minute); system.out.println( "minute =" + minute); // calendar.add(calendar.minute、15); minute = calendar.get(calendar.minute); system.out.println( "minute + 15 =" + minute); // calendar.add(calendar.minute、-30); minute = calendar.get(calendar.minute); System.out.println( "minute -30 =" + minut); // format display str =(new SimpledateFormat( "yyyy-mm-dd hh:mm:ss:ss"))。 System.out.println(str); //カレンダーのリセット現在の時刻カレンダーを表示します。Settime(new Date()); str =(new simpledateFormat( "yyyy-mm-dd hh:mm:ss:ss"))。 System.out.println(str); //時間を比較するためのカレンダーを作成してくださいカレンダーカレンダーnew = calendar.getInstance(); // 5時間前に設定すると、後者は大きく、-1 CalendarNew.Add(Calendar.Hour、-5)を表示します。 system.out.println( "時間比較:" + calendarynew.compareto(calendar)); // 7時間前に設定すると、前者は大きく、1 CalendarNew.Add(Calendar.Hour、+7)を表示します。 system.out.println( "時間比較:" + calendarynew.compareto(calendar)); // 2時間を返すと、時間は同じで、0 calendarnew.add(calendar.hour、-2)を表示します。 system.out.println( "時間比較:" + calendarynew.compareto(calendar)); }}時差を計算するには、Calendar.getTimeInmillis()を使用して2回のマイクロ秒の時差差を取得し、たとえばそれを変換して、位相差の日数を取得することができます。コードは次のとおりです。
//マイクロ秒の時間差を取得しますlong val = calendarEnd.getTimeInmillis() - CalendarBegin.getTimeInmillis(); //変換後の日数を取得します長い日= val /(1000 * 60 * 60 * 24);
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。