java对时间进行运算,java时间运算

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.Scanner;

public class Time {

public static void main(String[] args) throws ParseException {

SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Date startTime = ft.parse("1900-01-01 00:00:00");

Date endTime = ft.parse("2050-01-01 00:00:00");

System.out.println("请输入时间点(格式为yyyy-MM-dd hh:mm:ss):");

Scanner scanner=new Scanner(System.in);

String time= scanner.nextLine();

Date input = ft.parse(time);

boolean effectiveDate = isEffectiveDate(input, startTime, endTime);

if (effectiveDate) {

System.out.println("输入时间在范围内");

nextTime(input,ft);

} else {

System.out.println("输入时间在不在范围内");

}

}

//判断输入的时间是否在设置的时间内

public static boolean isEffectiveDate(Date input, Date startTime, Date endTime) {

if (input.getTime() == startTime.getTime()

|| input.getTime() == endTime.getTime()) {

return true;

}

Calendar date = Calendar.getInstance();

date.setTime(input);

Calendar begin = Calendar.getInstance();

begin.setTime(startTime);

Calendar end = Calendar.getInstance();

end.setTime(endTime);

if (date.after(begin) && date.before(end)) {

return true;

} else {

return false;

}

}

//输入时间3小时后的工作时间点,有休息时间就跳过

public static void nextTime( Date input, SimpleDateFormat ft){

Calendar that = Calendar.getInstance();

int nextTime=3;

int noonRest=2;

that.setTime(input);

int thatOur=Integer.parseInt(ft.format(that.getTime()).substring(11,13));//取出小时

if((thatOur<=9&&thatOur>=8)||(thatOur>=14&&thatOur<=15)){that.add(Calendar.HOUR,nextTime);}//不跳过休息时间

else if(thatOur<8)that.add(Calendar.HOUR,11-thatOur);//早上8点之前,直接跳到11点后

else if(thatOur<12)that.add(Calendar.HOUR,nextTime+noonRest);//12点之前,且会跳过休息时间

else if(thatOur<14)that.add(Calendar.HOUR,17-thatOur);//午间休息时间点,直接跳到17点后

else that.add(Calendar.HOUR,24-thatOur+11);//晚上下班后,直接跳到第二天11点后

System.out.println("增加3小时(跳过休息时间):"+ft.format(that.getTime()));

}

}

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

//获取当前时间

public class Time {

public static void main(String[] args) throws ParseException {

SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

System.out.println(ft.format(new Date()));

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值