java获取当前时间和时间比较大小

A获取当前时间:

 

Java代码
import java.text.SimpleDateFormat;  
import java.util.Date;  
 
public class GetCurrentTime {  
    /*获得当前时间*/ 
    public static String getCurrentTime(){  
        Date currentTime = new Date();  
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        String dateString = formatter.format(currentTime);  
        return dateString;  
    }  
 


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

public class GetCurrentTime {
/*获得当前时间*/
public static String getCurrentTime(){
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
return dateString;
}

}
B时间比较大小:

Java代码
import java.text.ParseException;  
import java.text.SimpleDateFormat;  
import java.util.Calendar;  
 
public class TimeCompare {  
    /*时间比大小*/ 
    public static int timeCompare(String t1,String t2){  
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        Calendar c1=Calendar.getInstance();  
        Calendar c2=Calendar.getInstance();  
        try {  
            c1.setTime(formatter.parse(t1));  
            c2.setTime(formatter.parse(t2));  
        } catch (ParseException e) {  
            e.printStackTrace();  
        }  
        int result=c1.compareTo(c2);  
        return result;  
    }  
 

你可能感兴趣的:(java)