关闭

java如何获得当前时间的前一天时间

473人阅读 评论(0) 收藏 举报
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);
  另有:
  Calendar 与 Date 的转换:
  Calendar calendar = Calendar.getInstance();
  // 从一个 Calendar 对象中获取 Date 对象
  Date date = calendar.getTime();
  // 将 Date 对象反应到一个 Calendar 对象中,
  // 先获得一个实例,然后设置 Date 对象
  calendar.setTime(date);
  月份的起始值为0而不是1,所以要设置八月时,我们用7而不是8.
  calendar.set(Calendar.MONTH, 7);
  计算2000-01-01是星期几
  calendar.get(calendar.DAY_OF_WEEK )
  Java代码
  import Java.util.*;
  public class ShowDate {
  public static void main(String[] args) {
  Calendar calendar = new GregorianCalendar();
  Date trialTime = new Date();
  calendar.setTime(trialTime);
  // print out a bunch of interesting things
  System.out.println("ERA: " + calendar.get(Calendar.ERA));
  System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
  System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
  System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
  System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
  System.out.println("DATE: " + calendar.get(Calendar.DATE));
  System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
  System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
  System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
  System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
  System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
  System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
  System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
  System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
  System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
  System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
  System.out.println("ZONE_OFFSET: " + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
  System.out.println("DST_OFFSET: " + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));
  System.out.println("Current Time, with hour reset to 3");
  calendar.clear(Calendar.HOUR_OF_DAY); // so doesn't override
  calendar.set(Calendar.HOUR, 3);
  System.out.println("ERA: " + calendar.get(Calendar.ERA));
  System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
  System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
  System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
  System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
  System.out.println("DATE: " + calendar.get(Calendar.DATE));
  System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
  System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
  System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
  System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));

0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:14255次
    • 积分:358
    • 等级:
    • 排名:千里之外
    • 原创:21篇
    • 转载:2篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论