String->date
String datestring = "2017-03-11"; Try { new SimpleDateFormat ("Yyyy-mm-dd"); = Sdf.parse (datestring);} Catch (ParseException e) { System.out.println (e.getmessage ());}
/*** String conversion to TIME format *@paramDatestr the string to be converted *@paramFormatstr Example of a target string requiring formatting Yyyy-mm-dd *@returndate returns the converted time *@throwsparseexception Conversion Exception*/ PrivateDate parsedate (String Strformat, String dateValue) {if(DateValue = =NULL) return NULL; if(Strformat = =NULL) Strformat= "YyyyMMdd"; DateFormat DateFormat=NewSimpleDateFormat (Strformat); Date Date=NULL; Try{Date=Dateformat.parse (DateValue); } Catch(parseexception pe) {date=NULL; } returndate; }
Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;Importjava.util.Date;Importorg.apache.commons.lang.StringUtils;/*** Date Util class * *@authorYK*/ Public classdateutil{Private StaticString Defaultdatepattern = "Yyyy-mm-dd"; /*** Get the default date pattern*/ Public StaticString Getdatepattern () {returnDefaultdatepattern; } /*** Returns the current date string for the preset format*/ Public StaticString Gettoday () {Date today=NewDate (); returnformat (today); } /*** Formatting date into strings using preset format*/ Public StaticString Format (date date) {returnDate = =NULL? " ": Format (date, Getdatepattern ()); } /*** Format date into a string using the parameter formats*/ Public Staticstring Format (date date, string pattern) {returnDate = =NULL? " " :NewSimpleDateFormat (pattern). Format (date); } /*** Convert a string to date using a preset format*/ Public StaticDate Parse (String strdate)throwsParseException {returnStringutils.isblank (strdate)?NULL: Parse (strdate, Getdatepattern ()); } /*** Use parameter format to convert a string to date*/ Public StaticDate Parse (string strdate, string pattern)throwsParseException {returnStringutils.isblank (strdate)?NULL:NewSimpleDateFormat (pattern). Parse (strdate); } /*** Add a few months to the date*/ Public StaticDate Addmonth (date date,intN) {Calendar cal=calendar.getinstance (); Cal.settime (date); Cal.add (Calendar.month, N); returnCal.gettime (); } Public Staticstring Getlastdayofmonth (string year, string month) {Calendar cal=calendar.getinstance (); //yearsCal.set (Calendar.year, Integer.parseint (year)); //month, because the calendar month is starting from 0, so to 1//Cal.set (Calendar.month, Integer.parseint (MONTH)-1); //Day, set to a numberCal.set (Calendar.date, 1); //month plus one, get a number next monthCal.add (Calendar.month, 1); //next one months minus one last day of the monthCal.add (Calendar.date, 1); returnString.valueof (Cal.get (calendar.day_of_month));//What's the date of the month } Public StaticDate GetDate (string year, string month, String day)throwsparseexception {String result= year + "-" + (month.length () = = 1? ("0" + month): month) + "-" + (day.length () = = 1? ("0" +Day) : day); returnparse (result); }}
Date->string
/** * Date Turn string * @param count @return*/ @SuppressWarnings ("unused") private String datetostring ( Date date) { return (new SimpleDateFormat ("YyyyMMdd")). Format (date);
PrivateDate AddDays (date date,intN) {Calendar cal=calendar.getinstance (); Cal.settime (date); //Cal.add (calendar.date, n);//N is the increase in the number of days that can be changedCal.add (Calendar.day_of_year, N);//hours plus 1Date =Cal.gettime (); returndate; } /*** A string that returns the day of the week based on a date *@paramDate *@return */ PrivateString Getweek (date date) {Calendar C=calendar.getinstance (); C.settime (date); //int Hour=c.get (calendar.day_of_week); //The hour is the day of the week, its range 1~7//1 = Sunday 7= Saturday, other analogy return NewSimpleDateFormat ("Eeee"). Format (C.gettime ()); }
Conversion and date type manipulation of Java string and date