1、 怎样格式化当前时间:格式化是要是想获取几天是今年的第多少天,把“dd”改成“DD”那个位置出现的就是今年的第多少天。
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
String currentDate = sdf.format(date);
2、获取一个日期是星期几:w 就是星期几。
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
3、这也是一个获取星期的方法:
public static String getWeek1(Date date){
String str = "";
Calendar c=Calendar.getInstance();
c.setTime(date);
int weekDay = c.get(Calendar.DAY_OF_WEEK);
if(Calendar.MONDAY == weekDay){
str = "星期一";
}else if(Calendar.TUESDAY==weekDay){
str = "星期二";
}else if(Calendar.WEDNESDAY==weekDay){
str ="星期三";
}else if(Calendar.THURSDAY==weekDay){
str ="星期四";
}else if(Calendar.FRIDAY==weekDay){
str ="星期五";
}else if(Calendar.SATURDAY==weekDay){
str ="星期六";
}else {
str ="星期日";
}
return str;
}
本文介绍了如何使用Java进行日期和时间的操作,包括格式化当前时间以获取今年的第多少天,以及确定给定日期是一周中的哪一天。通过具体代码示例展示了不同场景下日期时间的处理方法。
3753

被折叠的 条评论
为什么被折叠?



