Conversion of Java Data, String, long three date types to each other

Source: Internet
Author: User

Conversion of Java Data, String, long three date types to each other//Date type converted to String type //formattype format is YYYY-MM-DD hh:mm:ss//yyyy mm month dd Date HH mm min ss sec  //Data date type time   public static String date ToString (Date data, String formattype) {  return new SimpleDateFormat (formattype). Format (data); }   //long type to String type  //currenttime time of type of long to convert  //formattype time format of string type to convert   public static String longtostring (Long currenttime, string formattype)   throws parseexception {  Date date = Longtodate (Curre Nttime, formattype); The long type turns into date type   String strtime = datetostring (date, formattype); The date type is converted to string  return strtime; }  //String type to date type  //Strtime The time of the string type to be converted, Formattype format to convert yyyy-mm-dd hh:mm:ss//yyyy mm month DD day  //HH mm min SS, //Strtime time format must be the same as formattype time format   public static Date stringtodate (String strtime, String formattype)   throws ParseException {  Simpledatef Ormat Formatter = new SimpleDateFormat (formattype);  Date date = null;  Date = Formatter.parse (strtime);  return date; }  //long converted to date type  //currenttime time of long type to convert nbsp Formattype the time format to convert YYYY-MM-DD hh:mm:ss//yyyy mm month dd Date HH mm min ss sec   public static date longtodate (Long currenttime, String formattype)   throws parseexception {  Date dateold = new Date (currenttime);//based on the number of milliseconds of a long type of time of life a date type   String sdatetime = datetostring (Dateold, formattype); Converts the time of the date type to string  date date = Stringtodate (Sdatetime, formattype); Convert string type to date type   return date; }  //String type to Long type  //strtime time of String type to convert  //formattype time format  //strtime time format must be the same as the time format of the formattype   public static long Stringtolong (String strtime , string formattype)   throws parseexception {  Date date = Stringtodate (Strtime, formattype);//String type to date class Type   if (date = = null) {  return 0; } else {  long currenttime = Datetolong (date);//date type goes to long type &NB Sp Return Currenttime; } }  //date type converted to long type  //date of date type to be converted   public static long Datetolong (date dat e) {  return Date.gettime (); } reprinted: Http://www.blogjava.net/weishuangshuang/archive/2012/09/27/388712.html

Mutual conversions between Java Data, String, long three date types

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.