java中对date的一些处理以及获取date

在写项目的过程中,经常会遇到对日期的一些处理,或者对日期的获取

之前写的时候都是需要哪个百度查找,写上就没有下文了,这次我把自己遇到过得,写过的总结一下,相当于是一个记录

 private final static String YEAR_MONTH_DAY_HOURS_MINUTE_SECOND = "yyyy-MM-dd hh:mm:ss";
public static void main(String[] args) {
		try {
			getNowDate();
			String date2 = "2020-12-31 02:21:31";
			stringToDate(date2,YEAR_MONTH_DAY_HOURS_MINUTE_SECOND);
			dateToString(new Date(),YEAR_MONTH_DAY_HOURS_MINUTE_SECOND);
			currentWeekFirstDay();
			currentWeekLastDay();
			yesterDay(YEAR_MONTH_DAY_HOURS_MINUTE_SECOND);
			lastDay(YEAR_MONTH_DAY_HOURS_MINUTE_SECOND);
			currentMonthFirstDay();
			currentMonthLastDay();
			datetoWeek(new Date());
			dateofTheWeekToMap(new Date());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

  //获取当前时间

 public static Date getNowDate() throws Exception{
        Date date = new Date();
        return date;
    }

 //获取到昨天

public static String yesterDay(String dateType) throws Exception{
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE,-1);
        SimpleDateFormat sdf = new SimpleDateFormat(dateType);
        String dateString = sdf.format(calendar.getTime());
        return dateString;
    }

  //获取到明天

public static String lastDay(String dateType) throws Exception{
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE,1);
        SimpleDateFormat sdf = new SimpleDateFormat(dateType);
        String dateString = sdf.format(calendar.getTime());
        return dateString;
    }

 //String 转 date

 public static Date stringToDate(String dateString,String dateType) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat(dateType);
        Date date = sdf.parse(dateString);
        return date;
    }

//date 转 String

public static String dateToString(Date date,String dateType) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat(dateType);
        String dateString = sdf.format(date);
        return dateString;
    }

 //获取本周第一天

public static Date currentWeekFirstDay() throws Exception{
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.WEEK_OF_MONTH, 0);
        cal.set(Calendar.DAY_OF_WEEK, 2);
        Date date=cal.getTime();
        return date;
    }

  //获取本周最后一天

 public static Date currentWeekLastDay() throws Exception{
        Calendar cal=Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
        cal.add(Calendar.DAY_OF_WEEK, 1);
        Date date=cal.getTime();
        return date;
    }

 //据一个日期推算当周的每一天日期

 public static Map<String, Date> dateofTheWeekToMap(Date date) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        String str =  weekDays[w];
        List<Date> days = dateofWeekToList(date);
        Map<String,Date> dayWeekMap = new HashMap<>();
        for (Date d : days) {
            Calendar c = Calendar.getInstance();
            c.setTime(d);
            dayWeekMap.put(weekDays[c.get(Calendar.DAY_OF_WEEK)-1],d);
        }
        return dayWeekMap;
    }

 

 public static List<Date> dateofWeekToList(Date date) throws Exception{
        int b = date.getDay();
        Date oneDate;
        List<Date> list = new ArrayList<Date>();
        Long fTime = date.getTime() - b * 24 * 3600000;
        for (int a = 1; a <= 7; a++) {
            oneDate= new Date();
            oneDate.setTime(fTime + (a * 24 * 3600000));
            list.add(a-1, oneDate);
        }
        return list;
    }

   //获取本月第一天

public static Date currentMonthFirstDay() throws Exception{
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH,1);//1:本月第一天
        Date date = calendar.getTime();
        return date;
    }

  //获取本月最后一天

public static Date currentMonthLastDay() throws Exception{
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date date = calendar.getTime();
        return date;
    }

  //根据时间得到该时间是周几

public static String datetoWeek(Date date) throws Exception{
        String week = "";
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int weekday = c.get(Calendar.DAY_OF_WEEK);
        if (weekday == 1) {
            week = "星期日";
        } else if (weekday == 2) {
            week = "星期一";
        } else if (weekday == 3) {
            week = "星期二";
        } else if (weekday == 4) {
            week = "星期三";
        } else if (weekday == 5) {
            week = "星期四";
        } else if (weekday == 6) {
            week = "星期五";
        } else if (weekday == 7) {
            week = "星期六";
        }
        return week;
    }

 

 

另外准备了500套应用实战代码,关注二维码微信公众号,回复「java」获取(链接失效后关注下方微信公众号,回复「java」获取到微信给你发送)

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值