形式が正しい限り、文字列を直接比較するだけで、同じことが秒に当てはまります。
文字列S1 = "2003-12-12 11:30:24";文字列S2 = "2004-04-01 13:31:40"; int res = s1.compareto(s2);
日付の違いを見つけます
SimpleDateFormat df = new SimpledateFormat( "yyyy-mm-dd hh:mm:ss");日付begin = df.parse( "2004-01-02 11:30:24");日付終了= df.parse( "2004-03-26 13:31:40"); long oter =(end.getTime() - begin.getTime())/1000; // 1000でdividは秒int int day =/(24*3600); int hour =%(24*3600)/3600の間。 int minute =%3600/60の間。 int second =%60;
日付と日付のクラスがよく使用されるため、一般的に使用される日付の方法と属性は、すべての人がそれらを見つけるように促進するために次のように要約されています。
1.特定の月の最大日数を計算する
カレンダー時間= calendar.getInstance(); time.clear(); time.set(calendar.year、year); time.set(Calendar.month、i-1); //カレンダーオブジェクトは1月0日にデフォルトであることに注意してください。
注:SETメソッドを使用する前に、最初にクリアするのが最善です。そうしないと、多くの情報が現在のシステム時間から継承されます。
2。カレンダーと日付の変換
(1)これまでに変換されたカレンダー
カレンダーcal = charend.getInstance();日付date = cal.getTime();
(2)日付をカレンダーに変換します
日付date = new date();カレンダーcal = charend.getInstance(); Cal.Settime(日付);
3。出力日時をフォーマットします
日付date = new date(); SimpleDateFormat df = new SimpledateFormat( "yyyy-mm-dd hh:mm:ss"); System.out.println(df.format(date));
4.年の週を計算します
(1)1日の週の週を計算する
カレンダーcal = charend.getInstance(); Cal.set(Calendar.year、2006); cal.set(calendar.month、9); cal.set(calendar.day_of_month、3); int weekno = cal.get(calendar.week_of_year);
(2)今年の週を計算します
SimpleDateFormat df = new SimpledateFormat( "yyyy-mm-dd");カレンダーcal = charend.getInstance(); Cal.set(Calendar.year、2006); cal.set(calendar.week_of_year、1); cal.set(calendar.day_of_week、calendar.monday); system.out.println(df.format(cal.gettime()));
出力:
2006-01-02
5。add()とroll()の使用
(1)追加()メソッド
SimpleDateFormat df = new SimpledateFormat( "yyyy-mm-dd");カレンダーcal = charend.getInstance(); Cal.set(Calendar.year、2006); cal.set(calendar.month、9); cal.set(calendar.day_of_month、3); Cal.Add(calendar.date、-4);日付date = cal.getTime(); System.out.println(df.format(date)); Cal.Add(calendar.date、4); date = cal.getTime(); System.out.println(df.format(date));
出力:
2006-08-30
2006-10-03
(2)ロールメソッド
Cal.set(Calendar.year、2006); cal.set(calendar.month、9); cal.set(calendar.day_of_month、3); cal.roll(calendar.date、-4); date = cal.getTime(); System.out.println(df.format(date)); cal.roll(calendar.date、4); date = cal.getTime(); System.out.println(df.format(date));
出力:
2006-10-29
2006-10-03
今月内にロール()メソッドループがループし、add()メソッドが一般的に使用されていることがわかります。
6. 2回の任意の日数を計算する
(1)カレンダーオブジェクトに渡す
/*** // ** 2回の間の日数を計算* @param初期時間* @param終了時間*/public int getintervaldays(Calendarの初期、カレンダーの終わり).. {//終了日前(初日(終了日)).. sl = startday.getTimeInmillis(); long el = endday.getTimeInmillis(); long ei = el-sl; //ミリ秒リターン(int)(ei/(1000*60*60*24))に基づいて間隔日を計算します;}(2)日付オブジェクトに渡す
/*** // ** 2回の間の日数を計算* @param初期時間* @param endday endtime*/public int getintervaldays(date aintervaldays(date ainterday、date endday)... {// endday beendday if(astadday.after(endday)).. {date cal = cal = day = endday; sl = startday.getTime(); long el = endday.getTime(); long ei = el-sl; //ミリ秒リターン(int)(ei/(1000*60*60*24))に基づいて間隔日を計算します;}同様に、同じ方法を使用して時間数、分、秒などを計算できます。注:上記の方法は、時間に基づいて完全に計算されます。
lateday = "2006-10-11 20:00:00" endday = "2006-10-12 8:00:00"
計算結果は0ですが、計算結果が1になる場合があります。この時点で、次の方法を使用できます。
引数を渡す前に、次のような終わりの時間を最初に設定します。
endday.set(calendar.hour_of_day、23); endday.set(calendar.minute、59); endday.set(calendar.second、59); endday.set(calendar.millisecond、59);
あなたがそれを渡すならば、結果は私たちが望む通りになります。ただし、上記の方法が面倒な場合は、次の方法を参照できます。
(3)日数を正確に計算する方法を改善する
public getdaysbetween(カレンダーd1、カレンダーd2)... {if(d1.after(d2))... {// d1が開始され、d2がend java.util.calendar swap = d1; d1 = d2; d2 = swap;} int days = d2.get y2 = d2.get(calendar.year); if(d1.get(calendar.year)!= y2)... {d1 =(calendar)d1.clone(); do ... {days += d1.getactualmaximum(calendar.day_of_year); //実際の日数を取得します。 y2);} return days;}現在のシステム時間を取得します:
public static string getsystemtime(){date date = new date(); simpledateformat df = new simpledateformat( "yyyy-mm-dd hh:mm:ss"); return df.format(date);} //文字列をタイムタイプに変換します(文字列はあらゆるタイプになります。 sdf = new Java.Text.SimpleDateFormat( "M/DD/YYYY HH:MM:SS A"、java.util.util.us d = sdf.parse( "5/13/2003 10:31:37 A) HH:mm:ss "); string mdatetime1 = formatter.format(d); // calendar cal = calendar.getInstance(); // simpledateformat formatter = new simpledateFormat(" yyyy-mm-dd hh:mm:ss "); simpledateformat formatter = new simpledateformat( "yyyy-mm-dd hh:mm:ss gedf w w a ef"); string mdateTime = formatter.format(cal.gettime()); long mytime =(mydate.gettime()/1000)-60*60*24*365; mydate.settime(mytime*1000); string mdate = formatter.format(mydate); //明日の日付mydate = new java.util.date(); mytime =(mydate.gettime()/1000)+60*60*24; mydate.settime(mytime*1000); mdate = falatter.format(mydate); // 2倍単純化された日数= new simpledateformat( "yyymmdd"; myformatter.parse( "2003-05-1"); java.util.date mydate = myformatter.parse( "1899-12-30"); long day =(date.getTime.getTime())/(24*60*60*1000); // HH:mm:ss "); java.util.date date1 = format.parse(" 2002-02-28 23:16:00 "); long time =(date1.gettime()/1000)+60*30; date1.settime(time*1000); string mydate1 = formatter.format(date1); // year、month、week、date simpledateformat formatter2 = new SimpledateFormat( "yyyy-mm fe");金曜日 "); SimpleDateFormat formatter3 = new SimpleDateFormat(" yyyy-mm-dd "); string mydate2 = format3.format(date2); //週の日mydate = myformatter.parse(" 2001-1-1 ");Webアプリケーションの開発では、さまざまなデータベース日付タイプのプログラムで、さまざまな異なる変換を日付タイプにする必要があります。対応するデータベースデータがOracleの日付タイプ、つまり年、月、日だけが必要な場合、java.sql.dateタイプを使用することを選択できます。対応するデータベースデータがMSSQLServerデータベースのDateTimeタイプである場合、つまり年、月、日、時間、分、2番目が必要な場合は、java.sql.timestampタイプを選択します。
日付と日付の形式を定義し、文字列に変えることができます
パッケージパーソナル。Jessica; Import java.util.date; Import Java.util.calendar; Import java.sql.timestamp; Import java.text.dateformat; Import java.text.simpledateformat; Import Java.util.locale; class dertapt {/***simt the the the the the the the the the the the the the the the the timest the timests the timests the timests timests timests timests timest java.sql.timestamp) *@dateStampに変換する必要があるPARAM DATESTRING文字@@return datatimeタイムスタンプ */パブリックファイナルスタティックJava.sql.timestamp string2ime(string datestring)throws java.text.parseexception {dateformat dateformat; dateformat = New Simpledateforedformat KK:mm:ss.sss "、locale.english); // set format // dateformat = new simpledateFormat(" yyyy-mm-dd kk:mm:ss "、locale.english); dateformat.setlenient(false); java.util.date timedate = dateformat.parse(datestring); // utilタイプjava.sql.timestamp datetime = new java.sql.timestamp(timedate.gettime()); string typeの日付(java.sql.date) *@dateString日付に変換する必要がある文字列 *@return datatime date */public final static java.sql.date string2date(string datestring) locale.english); dateformat.setlenient(false); java.util.date timedate = dateformat.parse(datestring); // utilタイプjava.sql.date datetime = new java.sql.date(timedate.gettime()); date(); //注:この場所では、da.gettime()が長い値システムを取得します。 System.out.println(t); // 2番目の方法:タイムスタンプ(int year、int month、int date、int hour、int minute、int second、int nano)タイムスタンプ= new Timestame()。 calendar.getInstance()。get(calendar.hour)、calendar.getinstance()。 "2005-8-18 14:21:21:12.123"; // java.sql.timestampに変換するために使用されるstandle string date1 = string2date(stodate); timestamp = string2time(stotimestamp); system.out.println( "date:"+date1.tostring();日付date1 = string2date(stotimestamp); system.out.println( "date:"+date2.tostring()); // java.sql.timestamp date2 = string2time(stotimestamp); system.out.out.println( "+2.tostring("+2.tostring() "+2.tostring(); e){e.printstacktrace();}}}これが例です:
Javaコード
パッケージテスト; java.text.dateformat; Import java.text.parseexception; Import java.text.simpledateformat; Import java.util.calendar; Import java.util.date; Import java.util.hashtable; Import Javax.javax.swing; Joptionpane is bool eatic2 {public class boolean s){string a [] = s.split( " - "); boolean flg = true; if(!(integer.parseint(a [0]))> = 1950 && integer.parseint(a [0])<= 2050)){flg = false;} return flg;} boolean checkdate(string stry stry SimpleDateFormat( "yyyy-mm-dd"); ret = df.format(df.parse(s))。 hashtable(); fest.put( "01-01"、 "新年の日"); fest.put( "02-14"、 "バレンタインデー"); fest.put( "03-12"、 "arbor day"); fest.put( "03-15"、 "消費者の日"); fest put( "04-05")、 "" 4-05 ")フェスティバル "); fest.put(" 05-01 "、"労働者の日 "); fest.put(" 06-01 "、" Children's Day "); fest.put(" 07-01 "、" Party Day "); fest.put(" 08-01 "、" Army Day "); day "); fest.put(" 12-25 "、" christmas "); if(fest.containskey(a [1]))){return fest.get(a [1]);} else {return" no holiday ";}} public string xingzuo(date s){calendar cal = callean.getinstance(); cal. xingo =" day = cal.get(calendar.day_of_year); if((cal.get(calendar.year)%4 == 0)&&(cal.get(calendar.year)%100!= 0)||(cal.get(calendar.year)%400 == 0))))))))))) "scorpio";} else if(day> = 20 && day <= 49){xingzuo = "aquarius";} else if(day> = 50 && day <= 80){xingzuo = "pisces";} && day <= 141){xingzuo = "taurus";} else if(day> = 142 && day <= 173){xingzuo = "gemini";} else if(day> 174 && day <= 203){xingzuo = "cancer";} els> day> = 204 && <= 235) else if(day> = 236 && day <= 266){xingzuo = "virgo";} else if(day> = 267 && day <= 296){xingzuo = "libra";} else if(day> = 297 && day <= 326){xingzuo = "scorpio";} oth = 327 && day <= 356){xingzuo = "sauvignon";}} else {if((day> = 1 && day <= 19)||(day> = 357 && day <= 366)){xingzuo = "scorpio";}そうですif(day> = 49 && day <= 79){xingzuo = "pisces";} else if(day> = 80 && day <= 109){xingzuo = "aries";} else if(day> = 110 && day <= 140){xingzuo = "taurus"; "gemini";} else if(day> = 173 && day <= 202){xingzuo = "cancer";} && day <= 295){xingzuo = "libra";} else if(day> = 296 && day <= 325){xingzuo = "scorpio";} else if(day> = 326 && day <= 355){xingzuo = "saucy";} return xinguo;} s){SimpleDateFormat bartdateFormat = new SimpleDateFormat( "yyyy-mm-dd"); try {date 3 = bartdateformat.parse(s); date3 = bartdateformat.parse(s); return date3;} catch(Exception ex){return null;}} public void main = calb cal calendar.getInstance(); test2 test2 = new test2(); string date1 = joptionpane.showinputdialog( "日付を入力してください、形式は2000-10-15"); while((!(test2.checkdate(date1)&& test2.isdate(date1))){date1 = joptionpane.showinputdialog( "日付を入力してください、形式は2000-10-15") bartdateFormat2 = new SimpledateFormat( "yyyy-mm-dd"); date date2 = test2.parsedate(date1); string festinfo =(string)test2.dateinfo(date1); out.println(bartdateformat1.format(date2)+"; day = joptionpane.showinputdialog( "日付情報を入力してn days"); cal.settime(date2); cal.add(calendar.date、integer.parseint(day)); string date5 = bartdateformat2.format(cal.gettime()); festinfo =(string)test2.dateinfo(date5); system.out.println(bartdateformat1.format(cal.gettime()+"、"+festinfo+"、"+test2.xingo(cal.gettime());}要約します
上記は、この記事のJavaの時間と日付の使用法とクエリコードのすべての詳細な説明です。私はそれが誰にでも役立つことを願っています。興味のある友人は、このサイトの他の関連トピックを引き続き参照できます。欠点がある場合は、それを指摘するためにメッセージを残してください。このサイトへのご支援をありがとうございました!