Package Animaldemo;import Java.text.parseexception;import Java.text.simpledateformat;import java.util.Date;// Date Tool class public class Dateutil {public static final String defalt_adte_pattern= "Yyyy-mm-dd HH:mm:ss";p ublic static void Main ( String[] args) throws ParseException {System.out.println (datestring (New Date (), "Yyyy-mm-dd HH:mm:ss")); System.out.println (DateString (New Date (), null));} Private Dateutil () {}//-----------------------------------------------------------------------/* Determines whether the string is null or is an empty string */private the static Boolean IsBlank (String str) {return!haslength (str);} /* * Determine string non-null * Judge string NOT NULL, neither reference is NULL, nor null character * */private static Boolean haslength (String str) {///to encapsulate these two null-judged string methods into a class, it can be generalized. if (str!=null && "". Equals (Str.replace ("", "")) {//return true;//}//return false;return str!=null && ". Equals (Str.replace (" "") ");//Remove blank space to return true}//-------------------------------------------------------------- ---------//Format operation:--------------the date type >string type public static String datestring(Date date) {return datestring (date,null);} Format operation: the date type-------------->string type public static String datestring (date date,string pattern) {SimpleDateFormat Sdf=new SimpleDateFormat (); if (IsBlank (pattern)) {Pattern=defalt_adte_pattern;} Sdf.applypattern (pattern); return Sdf.format (date);} The parse operation puts the type String-------------->date type public static date StringDate (string date) throws Parseexception{return StringDate (date,null);} The parse operation puts the type string-------------->date type public static Date stringdate (string date,string pattern) throws Parseexception{simpledateformat sdf=new SimpleDateFormat (); if (IsBlank (pattern)) {Pattern=defalt_adte_pattern;} return Sdf.parse (date);}}
Import Java.util.calendar;import Java.util.date;public class Calendardemo {public static void main (string[] args) {//TOD O auto-generated method stub//requirements: Query last week ... Information about how to represent the last week. Date Current=new date ();//The current system time as the calendar Time C=calendar.getinstance (); c.settime;//Increase the day, the time, the Minutes, The second is set to 0c.add (Calendar.day_of_month, 1),//Plus one day c.set (calendar.hour_of_day, 0);//set to 0 c.set (calendar.minute, 0);// Set the score to 0 c.set (calendar.second, 0);//Set the second to 0Date Endtime=c.gettime (); C.add (Calendar.day_of_month, -7);D ate begintime= C.gettime (); System.out.println ("End Time:" +endtime.tolocalestring ()); System.out.println ("Start Time:" +begintime.tolocalestring ());}}
Java SimpleDateFormat Tool Class