日期:2014-05-18 浏览次数:20686 次
import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class TestDate { public static void main(String[] args) { // 设置当前日期 Calendar aCalendar = Calendar.getInstance(); aCalendar.setTime(new Date()); // 取当前日期是星期几(week:星期几) int week = aCalendar.get(Calendar.DAY_OF_WEEK); if (week == 1) { week = 7; } else if (week == 0) { week = 6; } else { week -= 1; } // 取距离当前日期最近的周日与当前日期相差的天数(count:相差的天数。正数:之后的周日,负数:之前的周日) int count = 0; if (week <= 3) { count = -week; } else if (week >= 4) { count = 7 - week; } // 获取距离当前日期最近的周日日期 DateFormat df = new SimpleDateFormat("yyyy-MM-dd EE"); Calendar c = Calendar.getInstance(); c.add(Calendar.DAY_OF_WEEK, count); // 格式化并打印出距离当前日期最近的周日日期 System.out.println("当前日期: " + df.format(aCalendar.getTime()) + '\n' + "最近周日: " + df.format(c.getTime())); } }