java如何获得当前时间的前一天时间
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));

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

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

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭