软件编程
位置:首页>> 软件编程>> java编程>> java获取当前时间和前一天日期(实现代码)

java获取当前时间和前一天日期(实现代码)

  发布时间:2023-09-17 07:48:35 

标签:java,日期

废话不多说,上代码


String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

Date dNow = new Date();   //当前时间
Date dBefore = new Date();

Calendar calendar = Calendar.getInstance(); //得到日历
calendar.setTime(dNow);//把当前时间赋给日历
calendar.add(Calendar.DAY_OF_MONTH, -1);  //设置为前一天
dBefore = calendar.getTime();   //得到前一天的时间


SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间格式
String defaultStartDate = sdf.format(dBefore);    //格式化前一天
String defaultEndDate = sdf.format(dNow); //格式化当前时间


System.out.println("前一天的时间是:" + defaultStartDate);

System.out.println("生成的时间是:" + defaultEndDate); 

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com