本文目录一览:
java中如何得到本周时间
import java.util.*;
public class DateTest {
public DateTest(){
Calendar c = Calendar.getInstance();
int weekday = c.get(7)-1;
c.add(5,-weekday);
System.out.println("本周开始时间:"+c.getTime());
c.add(5,6);
System.out.println("本周开始结束:"+c.getTime());
}
public static void main(String[] args) {
new DateTest();
}
}
Java中获取本周内所有日期
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class DataTest {
public static void main(String[] args) {
printWeekdays();
}
private static final int FIRST_DAY = Calendar.MONDAY;
private static void printWeekdays() {
Calendar calendar = Calendar.getInstance();
setToFirstDay(calendar);
for (int i = 0; i 7; i++) {
printDay(calendar);
calendar.add(Calendar.DATE, 1);
}
}
private static void setToFirstDay(Calendar calendar) {
while (calendar.get(Calendar.DAY_OF_WEEK) != FIRST_DAY) {
calendar.add(Calendar.DATE, -1);
}
}
private static void printDay(Calendar calendar) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd EE");
System.out.println(dateFormat.format(calendar.getTime()));
}
}
java怎么获取本周一到周末时间
/**
* 获取上周五时间
*/
public Date lastFirday() {
//作用防止周日得到本周日期
Calendar calendar = Calendar.getInstance();
while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
calendar.add(Calendar.DAY_OF_WEEK, -1);
}
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
int offset = 7 - dayOfWeek;
calendar.add(Calendar.DATE, offset - 9);
return DateTimeUtil.getFirstDayOfWeek(calendar.getTime(), 6);//这是从上周日开始数的到本周五为6
}
/**
* 获取上周一时间
*/
public Date lastMonday() {
Calendar calendar = Calendar.getInstance();
while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
calendar.add(Calendar.DAY_OF_WEEK, -1);
}
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
int offset = 1 - dayOfWeek;
calendar.add(Calendar.DATE, offset - 7);
return DateTimeUtil.getFirstDayOfWeek(calendar.getTime(), 2);
}
参考
请采纳,谢谢
JAVA中如何得到今天是星期几
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_WEEK);
String displayName = 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()),你自己运行一下就知道结果了