java获取星期几
❶ java怎么通过日期获取星期几
1、使用Calendar类
/**
* 获取当前日期是星期几<br>
*
* @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格式化日期
Date date=new Date();
SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
dateFm.format(date);
注:格式化字符串存在区分大小写
对于创建SimpleDateFormat传入的参数:EEEE代表星期,如“星期四”;MMMM代表中文月份,如“十一月”;MM代表月份,如“11”;
yyyy代表年份,如“2010”;dd代表天,如“25”
❷ java获取星期几
publicclassDates{
publicstaticvoidmain(String[]args){
java.text.SimpleDateFormatsdf=newjava.text.SimpleDateFormat("yyyy-MM-ddHH:mm:ss星期F");
System.out.println(sdf.format(newjava.util.Date()));
}
}
❸ 急:用java如何得到当前月的第一天是星期几
这个比较简单,4行代码就行
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 1);
SimpleDateFormat format = new SimpleDateFormat("E");
System.out.println("本月第一天是:" + format.format(calendar.getTime()));
如果你要得到一个数字的话就是:
calendar.get(Calendar.DAY_OF_WEEK),当然,这个结果是以星期天为第一天算出来的,如果要换成星期一开始,减1就行了
希望能帮助你。
❹ java获取当前日期前三位,即星期几
Calendar calendar = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-M-d");
String s = df.format(calendar.getTime());
System.out.println(s);
System.out.println(calendar.get(Calendar.DAY_OF_WEEK)-1);
因为星期是从周日开始的
❺ java中如何获取日期时间中的星期几
1、取得指定日期是星期几
取得指定日期是星期几可以采用下面两种方式取得日期是星期几:
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代表星期,如“星期四”;MMMM代表中文月份,如“十一月”;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、取得两个日期之间的相差多少天
已知两个日期,计算它们之间相差多少天。
// 取得两个日期之间的相差多少天
public static long getDaysBetween(Date date0, Date date1) {
long daysBetween = (date0.getTime() - date1.getTime() + 1000000) / 86400000;// 86400000=3600*24*1000 用立即数,减少乘法计算的开销
return daysBetween;
}
❻ java知道当前时间,怎样知道星期几
很简单的呀 用SimpleDateFormate的啦,这样写的啦 SimpleDateFormate sdf = new SimpleDateFormate("yyyy-MM-dd HH:mm:ss EEE");这个EEE就是星期几的啦 so easy的啦
❼ java如何实现获取一年中所有周的星期一和星期天。(要求1月4号所在的周为新年第一周)
代码截图如下:从2021-1-1到2021-12-31日
运行结果
❽ java怎样通过日期获取星期几
1、使用Calendar类
/**
* 获取当前日期是星期几<br>
*
* @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格式化日期
Date date=new Date();
SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
dateFm.format(date);
注:格式化字符串存在区分大小写
对于创建SimpleDateFormat传入的参数:EEEE代表星期,如“星期四”;MMMM代表中文月份,如“十一月”;MM代表月份,如“11”;
yyyy代表年份,如“2010”;dd代表天,如“25”
❾ JAVA中如何得到今天是星期几
publicstaticvoidmain(String[]args){
Calendarcalendar=Calendar.getInstance();
intday=calendar.get(Calendar.DAY_OF_WEEK);
StringdisplayName=calendar.getDisplayName(Calendar.DAY_OF_WEEK,Calendar.SHORT,Locale.getDefault());
System.out.println(day);
System.out.println(displayName);
}
可以通过calendar.get(Calendar.DAY_OF_WEEK)来获取今天在本周的索引值,从星期天开始,依次为1、2、3……到星期六为7。
或者通过本地化显示为当地的文字描述,通过calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.getDefault()),你自己运行一下就知道结果了