Date programming in Java

Source: Internet
Author: User

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 );
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.