java获得当前时间的前一天时间及Calendar 与 Date 的转换

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获得当前时间的前一天时间及Calendar 与 Date 的转换)