前言:这里整理一些关于时间方面的面试编程题
问题:取得当前时间的年月日,小时分秒。
方案:
[java]
/**
*输出现在时间
* @authorLiangGzone
* @version2013.04.10
*/
public class NowDate {
publicstatic void main(String[] args) {
Datenowdate = newDate();
SimpleDateFormatsdf = newSimpleDateFormat("yyyy-MM-dd hh:mm:ss");
StringnowdateStr = sdf.format(nowdate);
System.out.println("现在时间:"+nowdateStr);
}
}
/**
*输出现在时间
* @authorLiangGzone
* @version2013.04.10
*/
public class NowDate {
publicstatic void main(String[] args) {
Datenowdate = newDate();
SimpleDateFormatsdf = newSimpleDateFormat("yyyy-MM-dd hh:mm:ss");
StringnowdateStr = sdf.format(nowdate);
System.out.println("现在时间:"+nowdateStr);
}
}
问题:打印昨天的当前时刻
方案一:Date类
[java]
import java.text.*;
import java.util.*;
/**
*打印昨天的当前时刻
* @authorLiangGzone
* @version2013.04.10
*/
public class LastDate {
publicstatic void main(String[] args) {
DatenowDate = newDate();
longlastDateLong = nowDate.getTime()- 24 * 60 * 60 * 1000;
DatelastDate = newDate(lastDateLong);
SimpleDateFormatsdf = newSimpleDateFormat("yyyy-MM-dd hh:mm:ss");
StringlastDateStr = sdf.format(lastDate);
System.out.println(lastDateStr);
}
}
import java.text.*;
import java.util.*;
/**
*打印昨天的当前时刻
* @authorLiangGzone
* @version2013.04.10
*/
public class LastDate {
publicstatic void main(String[] args) {
DatenowDate = newDate();
longlastDateLong = nowDate.getTime()- 24 * 60 * 60 * 1000;
DatelastDate = newDate(lastDateLong);
SimpleDateFormatsdf = newSimpleDateFormat("yyyy-MM-dd hh:mm:ss");
StringlastDateStr = sdf.format(lastDate);
System.out.println(lastDateStr);
}
}
方案二:Canlendar
[java]
/**
*打印昨天的当前时刻
* @authorLiangGzone
* @version2013.04.10
*/
public class LastDate2 {
publicstatic void main(String[] args) {
Calendarcalendar = Calendar.getInstance();
SimpleDateFormatsdf = newSimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//计算出昨天的当前时刻
calendar.add(Calendar.DAY_OF_YEAR,-1);
//时间格式化
StringlastDateStr = sdf.format(calendar.getTime());
System.out.println("昨天当前时刻:"+lastDateStr);
}
}
/**
*打印昨天的当前时刻
* @authorLiangGzone
* @version2013.04.10
*/
public class LastDate2 {
publicstatic void main(String[] args) {
Calendarcalendar = Calendar.getInstance();
SimpleDateFormatsdf = newSimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//计算出昨天的当前时刻
calendar.add(Calendar.DAY_OF_YEAR,-1);
//时间格式化
StringlastDateStr = sdf.format(calendar.getTime());
System.out.println("昨天当前时刻:"+lastDateStr);
}
}
类似地,打印后天的当前时刻?打印前年的当前时刻?
问题:计算两个时间段相距天数
方案:
[java]
/**
*计算两个时间段相距天数
* @authorLiangGzone
* @version2013.04.10
*/
public class TwoDate {
publicstatic void main(String[] args) throws Exception {
StringoldStr = "2000-9-9";
StringnewStr = "2000-8-8";
longoldLong = 0;
longnewLong = 0;
DateFormatdateFormat = DateFormat.getDateInstance();
oldLong= dateFormat.parse(oldStr).getTime();
newLong= dateFormat.parse(newStr).getTime();
longresult = Math.abs(oldLong-newLong);
System.out.println(result/24/60/60/1000);
}
}
/**
*计算两个时间段相距天数
* @authorLiangGzone
* @version2013.04.10
*/
public class TwoDate {
publicstatic void main(String[] args) throws Exception {
StringoldStr = "2000-9-9";
StringnewStr = "2000-8-8";
longoldLong = 0;
longnewLong = 0;
DateFormatdateFormat = DateFormat.getDateInstance();
oldL