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;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。