Java中日期的处理方法(部分)


前言

总结了日常开发过程中遇到的部分对于日期的处理方法。
本文只是针对个人实际项目做出的开发记录,无其他参考意义。


一、计算日期的星期

public class Date{
	public static void main(String[] args) throws ParseException {
		String date = "2022070809";
		System.out.println(Date.getWeek(date));
	}
	
	public static String getWeek(String date) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
        // 获得一个日历
        Calendar cal = Calendar.getInstance();
        Date dateweek = null;
        try {
            dateWeek = simpleDateFormat.parse(date.substring(0,8));
            cal.setTime(dateWeek);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // 指示一个星期中的某天。
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
		}
	}

二、计算日期此后的某天

public class Date{
	public static void main(String[] args) throws ParseException {
		String date = "2022070809";
		System.out.println(Date.getNext(date, n));
	}
	
	public static String getNext(String date, int n) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
		String dateNext;
		//获得一个日历
		Calendar cal = Calendar.getInstance();
		Date datet = null;
		try {
			datet = simpleDateFormat.parse(date.substring(0,8));
			cal.setTime(datet);
		} catch (ParseException e) {
			e.printStackTrace();
			}
		cal.add(Calendar.DATE, n);
		//计算月
		//cal.add(Calendar.MONTH, n);
		dateNext = simpleDateFormat.format(cal.getTime());
		return dateNext;
		}
	}

三、时间转换时间戳

public class Date{
	public static void main(String[] args) throws ParseException {
		String date = "2022070809";
		System.out.println(Date.timeConvert(date));
	}
	
	public String timeConvert(String date) throws ParseException {
        String dateStamp;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHH");
        Date dateStamps = simpleDateFormat.parse(date);
        long ts = dateStamps.getTime() / 1000;
        dateStamp = String.valueOf(ts);
        return dateStamp;
    }

四、获取系统时间

public class Date{
	public static void main(String[] args) throws ParseException {
		System.out.println(Date.getSystemDate());
	}
	
	public String getSystemDate(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHH");
        Date date = new Date();
        String format = simpleDateFormat.format(date);
        return format;
    }

五、判断字符串日期格式

(一)判断是否非法,返回布尔类型

public class Date{
	public static void main(String[] args) throws ParseException {
		String date = "2022080118";
		System.out.println(Date.isInvalidDate(str));
	}
	
	public boolean isInvalidDate(String str){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHH");
        try {
            Date date = formatter.parse(str);
            return !str.equals(simpleDateFormat.format(date));
        }catch (Exception e){
            return true;
        }
    }

(二)判断是否合法或存在,返回日期或系统日期

public class Date{
	public static void main(String[] args) throws ParseException {
		String date = "2022080118";
		System.out.println(Date.isValidDate(str));
	}
	
	public static String isValidDate(String str) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHH");
        Date date = simpleDateFormat.parse(str);
        if (str.equals(simpleDateFormat.format(date))){
            return str;
            }
        else {
            Date currentdate = new Date();
            return simpleDateFormat.format(currentdate);
        }
    }

六、计算时间差

public class Date{
	public static void main(String[] args) throws ParseException {
		String reftime = "2022083100";
		String time = "2022090110";
		System.out.println(timeDiff(reftime,time));
	}
	
    public static timeDiff(String reftime, String time)throws ParseException{
    	SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHH");
    	Date reftime1 = (Date) simpleDateFormat.parse(reftime);
    	Date time1 = (Date) simpleDateFormat.parse(time);
    	long diff = time1.getTime() - reftime1.getTime();
    	return diff/60/60/1000;//小时差
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XxHoo_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值