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