Conversion between Date and String/Long, datestring

Source: Internet
Author: User

Conversion between Date and String/Long, datestring

Below are several common types of conversions about dates:

Import java. text. parseException; import java. text. simpleDateFormat; import java. util. date; public class Date_String {@ SuppressWarnings ("deprecation") public static void main (String [] args) {SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd "); try {Date date = sdf. (parse ("5689741236589745631"); System. out. println ("date =" + date); // error message, because the parameter string in parse is not a specific string} catch (ParseException e) {e. the parse () method in the printStackTrace ();}/*** SimpleDateFormat class is used to convert a specific input string to an object of the Date class * parse () without a parameter, when converting a specific String to a Date class * Using parse () without parameters, the String to be parsed must follow the "Year-month-day (there must be a space) morning/afternoon hours: Minutes "* e. g.: 2011-11-21 am-12 11-10-2 am * The converted Date class is also in the system format, with poor readability */Date date Date = null; string s = ""; SimpleDateFormat sdf1 = new SimpleDateFormat (); try {date = sdf1.parse (s); System. out. println ("------ the System does not have the parse parameter Date ------" by default); System. out. println ("" + date); // Sun Jan 29 00:03:00 CST 2012} catch (ParseException e) {e. printStackTrace ();}/*** use the custom format SimpleDateFormat (String pattern) to convert the String to the Date Class Using parse (). * The Date class is also the system format, poor readability */Date date1 = null; String s1 = "2012-01-29-22-26-23 "; string pattern = "yyyy-MM-dd-HH-mm-ss"; SimpleDateFormat sdf11 = new SimpleDateFormat (pattern); try {date1 = sdf11.parse (s1); System. out. println ("------ parse ------ custom Date Format"); System. out. println ("" + date1); // Sun Jan 29 22:26:23 CST 2012} catch (ParseException e) {e. printStackTrace ();} // The creation parameter is a Long-type Date d = new Date (System. currentTimeMillis (); System. out. println ("d =" + d. toLocaleString (); // d = 13:49:29 // convert Date to Long to get the number of milliseconds in the current time long time = d. getTime (); System. out. println ("time =" + time); // time = 1475041769413 int date11 = d. getDate (); // 1-31 System. out. println ("date1 =" + date11); // date1 = 28 int day = d. getDay (); // 0-6, 0 indicates Sunday 6 indicates Saturday System. out. println ("day =" + day); // day = 3 int month = d. getMonth (); // 0-11, 0 indicates January 1, January, and 11 indicates January 1, December. out. println ("month =" + month); // month = 8 int year = d. getYear (); // The number of years from January 1, 1900 to System. out. println ("year =" + year); // year = 116 int hour = d. getHours (); // System of the current date. out. println ("hour =" + hour); // hour = 13 int minute = d. getMinutes (); // a fraction of the current date of the System. out. println ("minute =" + minute); // minute = 49 int second = d. getSeconds (); // The number of seconds of the current date System. out. println ("second =" + second); // second = 29 // convert Date to String newDate = ""; newDate = sdf. format (d); System. out. println ("newDate =" + newDate); // newDate = 2016-09-28 }}

 

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.