I will sort out some interview programming questions on time.
Problem: Get the year, month, day, hour, minute, and second of the current time.
Solution:
[Java]
/**
* Output current time
* @ 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 ("current time:" + nowdateStr );
}
}
/**
* Output current time
* @ 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 ("current time:" + nowdateStr );
}
}
Problem: print the current time of yesterday
Solution 1: Date class
[Java]
Import java. text .*;
Import java. util .*;
/**
* Print the current time of yesterday
* @ 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 .*;
/**
* Print the current time of yesterday
* @ 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 );
}
}
Solution 2: Canlendar
[Java]
/**
* Print the current time of yesterday
* @ AuthorLiangGzone
* @ Version2013.04.10
*/
Public class LastDate2 {
Publicstatic void main (String [] args ){
Calendarcalendar = Calendar. getInstance ();
SimpleDateFormatsdf = newSimpleDateFormat ("yyyy-MM-dd hh: mm: ss ");
// Calculate the current time of yesterday
Calendar. add (Calendar. DAY_OF_YEAR,-1 );
// Time format
StringlastDateStr = sdf. format (calendar. getTime ());
System. out. println ("current time yesterday:" + lastDateStr );
}
}
/**
* Print the current time of yesterday
* @ AuthorLiangGzone
* @ Version2013.04.10
*/
Public class LastDate2 {
Publicstatic void main (String [] args ){
Calendarcalendar = Calendar. getInstance ();
SimpleDateFormatsdf = newSimpleDateFormat ("yyyy-MM-dd hh: mm: ss ");
// Calculate the current time of yesterday
Calendar. add (Calendar. DAY_OF_YEAR,-1 );
// Time format
StringlastDateStr = sdf. format (calendar. getTime ());
System. out. println ("current time yesterday:" + lastDateStr );
}
}
Similarly, what is the current time of the day after tomorrow? Print the current time of the year before?
Problem: calculate the number of days between two time periods
Solution:
[Java]
/**
* Calculate the number of days between two time periods.
* @ 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 );
}
}
/**
* Calculate the number of days between two time periods.
* @ 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 );
}
}