Java的三种时间

package com.cctchina.test;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


public class TimeUtil {
    
    public static void main(String[] args) {
        Date date = new Date() ;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        
        System.out.println("The first method:");
      //当前时间
        String currentTime = format.format(date);
        //   24小时之前的时间
        date.setDate(date.getDate()-1);
        String befor24Time = format.format(date);
        
        
        System.out.println("currentTime:" +currentTime);
        System.out.println("befor24Time:" +befor24Time);
        
        System.out.println("The second method:");
        Calendar calendar = Calendar.getInstance();
        System.out.println("currentTime:" +format.format(calendar.getTime()));
        //   24小时之前的时间
        calendar.add(Calendar.DATE, -1);
        System.out.println("befor24Time:" +format.format(calendar.getTime()));
        
  
        GregorianCalendar time = new GregorianCalendar();    
        System.out.println("The third method:");
        System.out.println("currentTime:" +format.format(time.getTime()));
        time.add(GregorianCalendar.DATE,-1);
        System.out.println("befor24Time:" +format.format(time.getTime()));
       
    }
}

你可能感兴趣的:(java)