日期和星期工具类

来源:互联网 发布:托福 一个月 知乎 编辑:程序博客网 时间:2024/06/13 22:20
public class TimeTest {public static void main(String[] args) {// 定义输出日期格式,Locale.CHINESE是为了防止在Linux服务器上出现星期英文SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd EEE",Locale.CHINESE);Date currentDate = new Date();// 比如今天是2017-07-12List<Date> days = dateToWeek(currentDate);System.out.println("今天的日期: " + sdf.format(currentDate));for (Date date : days) {String dateString = sdf.format(date);String[] split = dateString.split(" ");String mouth = split[0];String week = split[1];System.out.println(mouth + "---" + week);}}/** * 根据日期获得所在周的日期 *  * @param mdate * @return */@SuppressWarnings("deprecation")public static List<Date> dateToWeek(Date mdate) {int b = mdate.getDay();Date fdate;List<Date> list = new ArrayList<Date>();Long fTime = mdate.getTime() - b * 24 * 3600000;for (int a = 1; a <= 7; a++) {fdate = new Date();fdate.setTime(fTime + (a * 24 * 3600000));list.add(a - 1, fdate);}return list;}}

原创粉丝点击