JAva取得当日曜日
5696 ワード
簡単で乱暴な2つの方法
1、自分で1つの方法を書いて日付を取って余剰を取る
2、SimpleDateFormatの入力を作成するパラメータ:EEEEは「金曜日」などの曜日を表します.MMMMMMは中国語の月を代表して、例えば“11月”;MMは月を代表して、例えば“10”;yyyyは「2017」などの年を表します.ddは天を表し、例えば「28」
に注意 に注意 に注意 : 大文字と小文字を区別!!!
開発では、いくつかの日付に関する操作がよく使用されます.次の例では、いくつかの一般的な操作を示します.
1、取得指定日は曜日取得指定日は曜日は次の2つの方法で取得できます.
a、Calendarクラスの使用
b、SimpleDateFormatクラスの使用
注意:フォーマット文字列には、SimpleDateFormatを作成するために大文字と小文字を区別するパラメータがあります.EEEEは木曜日などの曜日を表します.MMMMMMは中国語の月を代表して、例えば“11月”;MMは月を代表して、例えば“11”;yyyyは「2010」などの年を表します.ddは天を表し、例えば「25」
2、取得日は、ある年の何週目日付により、ある年の何週目となります.
3、ある年のある月が何日あるか知っている年と月が何日あるか、その月が何日あるかを得る.
4、2つの日付の間の差を取得する日2つの日付が既知で、それらの間の差を計算します.
5、完全なテストコード
6、テスト結果
7、引き出した問題は以下のコードを見てください.
Javaではyyyy-MM-ddの日付フォーマットで変換され、変換文字列が999-999-999の場合、異常はなく、むしろ実行が通過した.
運転結果:1084-11-23
解決策は次のとおりです.
date=format.parse(strDate)の前にformat.setLenient(false)を付ければいいです.日/時間解析が厳密でないかどうかを指定します.厳密でない解析を行う場合、解析プログラムは、このオブジェクトのフォーマットと正確に一致しない入力を啓発的な方法で説明できます.厳密な解析を行う場合は、このオブジェクトのフォーマットに一致する必要があります. 】 ---------------------
1、自分で1つの方法を書いて日付を取って余剰を取る
/**
*
*
* @param dt
* @return
*/
public static String getWeekOfDate(Date dt) {
String[] weekDays = {" ", " ", " ", " ", " ", " ", " "};
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
2、SimpleDateFormatの入力を作成するパラメータ:EEEEは「金曜日」などの曜日を表します.MMMMMMは中国語の月を代表して、例えば“11月”;MMは月を代表して、例えば“10”;yyyyは「2017」などの年を表します.ddは天を表し、例えば「28」
に注意 に注意 に注意 : 大文字と小文字を区別!!!
Date date=new Date();
SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
dateFm.format(date);
開発では、いくつかの日付に関する操作がよく使用されます.次の例では、いくつかの一般的な操作を示します.
1、取得指定日は曜日取得指定日は曜日は次の2つの方法で取得できます.
a、Calendarクラスの使用
//
public static String getWeek(Date date){
String[] weeks = {" "," "," "," "," "," "," "};
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
if(week_index<0){
week_index = 0;
}
return weeks[week_index];
}
b、SimpleDateFormatクラスの使用
//
public static String getWeek(Date date){
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
String week = sdf.format(date);
return week;
}
注意:フォーマット文字列には、SimpleDateFormatを作成するために大文字と小文字を区別するパラメータがあります.EEEEは木曜日などの曜日を表します.MMMMMMは中国語の月を代表して、例えば“11月”;MMは月を代表して、例えば“11”;yyyyは「2010」などの年を表します.ddは天を表し、例えば「25」
2、取得日は、ある年の何週目日付により、ある年の何週目となります.
//
public static int getWeekOfYear(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int week_of_year = cal.get(Calendar.WEEK_OF_YEAR);
return week_of_year;
}
3、ある年のある月が何日あるか知っている年と月が何日あるか、その月が何日あるかを得る.
//
public static int getDaysOfMonth(int year,int month){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month-1);
int days_of_month = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
return days_of_month;
}
4、2つの日付の間の差を取得する日2つの日付が既知で、それらの間の差を計算します.
//
public static long getDaysBetween(Date date0, Date date1) {
long daysBetween = (date0.getTime() - date1.getTime() + 1000000) / 86400000;// 86400000=3600*24*1000 ,
return daysBetween;
}
5、完全なテストコード
package org.ml.test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class CalendarDemo {
public static void main(String[] args) {
String strDate = "2013-03-08";//
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//
Date date = null;
try {
date = format.parse(strDate);//
} catch (ParseException e) {
System.out.println(" !");
}
System.out.println(strDate + " :" + getWeek(date));
System.out.println(strDate + " :" + getWeekOfYear(date) + " ");
System.out.println(strDate + " " + (date.getMonth() + 1) + " :"
+ getDaysOfMonth(date.getYear(), date.getMonth() + 1) + " ");
System.out.println(strDate + " " + (format.format(new Date())) + " "
+ getDaysBetween(date, new Date()) + " ");
}
//
public static String getWeek(Date date) {
// String[] weeks = {" "," "," "," "," "," "," "};
// Calendar cal = Calendar.getInstance();
// cal.setTime(date);
// int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
// if(week_index<0){
// week_index = 0;
// }
// return weeks[week_index];
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
String week = sdf.format(date);
return week;
}
//
public static int getWeekOfYear(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int week_of_year = cal.get(Calendar.WEEK_OF_YEAR);
return week_of_year;
}
//
public static int getDaysOfMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month-1);
int days_of_month = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
return days_of_month;
}
//
public static long getDaysBetween(Date date0, Date date1) {
long daysBetween = (date0.getTime() - date1.getTime() + 1000000) / 86400000;// 86400000=3600*24*1000 ,
return daysBetween;
}
}
6、テスト結果
7、引き出した問題は以下のコードを見てください.
public static void main(String[] args) throws Exception{
String strDate = "999-999-999";//
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//
Date date = null;
try {
date = format.parse(strDate);//
} catch (ParseException e) {
System.out.println(" , ");
return;
}
System.out.println(format.format(date));
}
Javaではyyyy-MM-ddの日付フォーマットで変換され、変換文字列が999-999-999の場合、異常はなく、むしろ実行が通過した.
運転結果:1084-11-23
解決策は次のとおりです.
date=format.parse(strDate)の前にformat.setLenient(false)を付ければいいです.日/時間解析が厳密でないかどうかを指定します.厳密でない解析を行う場合、解析プログラムは、このオブジェクトのフォーマットと正確に一致しない入力を啓発的な方法で説明できます.厳密な解析を行う場合は、このオブジェクトのフォーマットに一致する必要があります. 】 ---------------------