Java Date Action Tool class (get specified date, date conversion, days apart) _java

Source: Internet
Author: User
Tags date1 string format

This example of this article for you to share the Java Date Operations Tool class, get the specified date the day before, the following days, date conversion, two dates, and other tools for reference, the details are as follows

Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import java.util.ArrayList;
Import Java.util.Calendar;
Import Java.util.Date;
 
Import java.util.List; public class Dateutil {/** * Gets the day before the specified date * * @param specifiedday * @param format * @return * @thr 
    OWS Exception */public static Date Getdaybefore (date date, String format) {Calendar c = calendar.getinstance ();
    C.settime (date);
    int day = C.get (calendar.date);
 
    C.set (Calendar.date, day-1);
     
    String daybeforestr = new SimpleDateFormat (format). Format (C.gettime ());
    Date daybefore = null;
    try {daybefore = new SimpleDateFormat (format). Parse (DAYBEFORESTR);
    catch (ParseException e) {e.printstacktrace ();
  return daybefore; /** * Gets the day after the specified date * * @param specifiedday * @return/public static date Getdayafter (Date da
    TE, String format) {Calendar c = calendar.getinstance ();
    C.settime (date); Int Day = C.get (calendar.date);
    C.set (calendar.date, day + 1);
    String dayafterstr = new SimpleDateFormat (format). Format (C.gettime ());
    Date dayafter = null;
    try {dayafter = new SimpleDateFormat (format). Parse (DAYAFTERSTR);
    catch (ParseException e) {e.printstacktrace ();
  return dayafter;
   /** * The day before the specified date * * @param specifiedday * @param format * @return * @throws Exception */public static string Getspecifieddaybefore (string specifiedday, string format) {//SimpleDateFormat Simpledatef
    Ormat = new//SimpleDateFormat ("Yyyy-mm-dd");
    Calendar C = calendar.getinstance ();
    Date date = null;
    try {date = new SimpleDateFormat ("Yy-mm-dd"). Parse (Specifiedday);
    catch (ParseException e) {e.printstacktrace ();
    } c.settime (date);
    int day = C.get (calendar.date);
 
    C.set (Calendar.date, day-1); String Daybefore = new SimpleDateFormat ("Yyyy-mm-dd"). Format (C.gettime ());
  return daybefore; /** * Get the day after the specified date * * @param specifiedday * @return/public static String Getspecifieddayafter (St
    Ring Specifiedday, String format) {Calendar c = calendar.getinstance ();
    Date date = null;
      try {//date = new SimpleDateFormat ("Yy-mm-dd"). Parse (Specifiedday);
    Date = new SimpleDateFormat (format). Parse (Specifiedday);
    catch (ParseException e) {e.printstacktrace ();
    } c.settime (date);
    int day = C.get (calendar.date);
 
    C.set (calendar.date, day + 1);
    String dayafter=new//SimpleDateFormat ("YYYY-MM-DD"). Format (C.gettime ());
    String dayafter = new SimpleDateFormat (format). Format (C.gettime ());
  return dayafter; /** * To specify the date type as a string in the specified format format * * @param day date * @param format specified * @return/public stat
    IC string date2string (Date day, String format) {string datestr = new SimpleDateFormat (format). Format (Day.gettime ());
  return datestr; }
 
  /** * Converts a string to the date type in the specified format * * @param day date * @param format specify format * @return/public static Date s
    Tring2date (String datestr,string format) {Date strdate = null;
    try {strdate = new SimpleDateFormat (format). Parse (DATESTR);
    catch (ParseException e) {e.printstacktrace ();
  return strdate; /** * * @Title: Datedate2comparevalue * @Description: Two date types Compare size * @param dayafter Date type * @pa Ram Date Date Type * @param format * @return Boolean * @throws/public static Boolean Datedate2comparevalue (Dat
    e Dayafter, date date, String pattern) {SimpleDateFormat SDF = new SimpleDateFormat (pattern);
      try {Date D1 = sdf.parse (Sdf.format (dayafter));
      Date D2 = Sdf.parse (Sdf.format (date));
      if (D1.gettime () >=d2.gettime ()) {return true;
    } catch (ParseException e) {e.printstacktrace ();
  return false; /** * Compare two dates month/year size * @Title: DatecompArevalue * @Description: A stirng A date type comparison size * @param date1 String type * @param date2 date type * @param @return * @return Boolean * @throws/public static Boolean Datestrdatecomparevalue (string date1, date date, string Patt
    Ern) {SimpleDateFormat SDF = new SimpleDateFormat (pattern);
      try {Date D1 = sdf.parse (date1);
      Date D2 = Sdf.parse (Sdf.format (date));
      if (D1.gettime () >=d2.gettime ()) {return true;
    The catch (ParseException e) {//TODO auto-generated catch block E.printstacktrace ();
  return false; /** * Compare the size of two date and month * @Title: Datestr2comparevalue * @Description: Two string types compare time Size * @param date1 * @par Am Date2 * @return Boolean * @throws */@SuppressWarnings ("unused") public static Boolean Datestr2comparevalu
  E (String date1, string date2,string pattern) {SimpleDateFormat SDF = new SimpleDateFormat (pattern);
    try {Date D1 = sdf.parse (date1); Date D2 = sdf.Parse (DATE2);
    if (D1.gettime () >=d2.gettime ()) {return true;
  } catch (ParseException e) {e.printstacktrace ();
  return false; /** * * @Title: Get2datelistdate * @date May 17, 2016 afternoon 2:11:48 * @Description: Date collection of days separated by the time of acquisition * @param @param predate start time * @param @param nextdate End time * @param @param format fomat format * @param @return * @ret Urn list<date> number of days separated by * @throws/public static list<date> Get2datelistdate (string predate, string NE
    Xtdate, String format) {list<date> List = new arraylist<> ();
      if (Nextdate.equals (predate)) {//Start time end time is equal to return directly to List.add (Dateutil.string2date (predate, format));
    return list;
    String nexdate = dateutil.getspecifieddayafter (predate, format);
    List.add (dateutil.string2date (predate, format));
    List.add (Dateutil.string2date (nexdate, format)); while (!nexdate.equals (nextdate)) {String nextnextdate = Dateutil.getspecifiEddayafter (nexdate, format);
      List.add (Dateutil.string2date (nextnextdate, format));
    Nexdate = nextnextdate;
  } return list;
  /** * * @Title: Get2datelistdate * @date May 17, 2016 afternoon 2:11:48 * @Description: Get a string collection of days separated by time * @param @param predate start time * @param @param nextdate End time * @param @param format fomat format * @param @return * @r Eturn list<date> separated days set * @throws/public static list<string> Get2datelistdatestr (String predate, St
    Ring nextdate, String format) {list<string> List = new arraylist<> ();
      if (Nextdate.equals (predate)) {//If the start time equals the end time then the same day list.add (predate);
    return list;
    String nexdate = dateutil.getspecifieddayafter (predate, format);
    List.add (predate);
    List.add (nexdate);
      while (!nexdate.equals (nextdate)) {String nextnextdate = dateutil.getspecifieddayafter (nexdate, format);
      List.add (nextnextdate);
    Nexdate = nextnextdate;
 }   return list;  /** * * @Title: Get2datelistwithdate * @date May 26, 2016 morning 9:20:29 * @Description: Get the date between two dates * @param @param startdate Start date type * @param @param enddate End Date Type * @param @return * @return list<date&  Gt Datelist * @throws/public static list<date> get2datelistwithdate (date startdate, date enddate,string forma
    T) {list<date> List = new arraylist<> ();
    if (Startdate.gettime () >enddate.gettime ()) {return list;
    String startdatestr = dateutil.date2string (startdate, format);
    String enddatestr = dateutil.date2string (enddate, format);
      if (Startdatestr.equals (ENDDATESTR)) {//If the start time equals the end time then the same day List.add (StartDate);
    return list;
    Date nextdate = dateutil.getdayafter (startdate, format);
    String nextdatestr = dateutil.date2string (nextdate, format);
    String enddatestr = dateutil.date2string (enddate, format);
    List.add (StartDate); List.add (NextDate);
      while (!nextdatestr.equals (ENDDATESTR)) {String nextnextdate = dateutil.getspecifieddayafter (nextdatestr, format);
      List.add (Dateutil.string2date (nextnextdate, format));
    Nextdatestr = nextnextdate;
  } return list; public static void Main (string[] args) throws Exception {/**/String predate = Getspecifieddaybefore (
    "2016-05-01", "yyyy-mm-dd");
 
    String nextdate = Getspecifieddayafter ("2016-05-03", "yyyy-mm-dd");
    Date Befroday = Getdaybefore (new date (), "yyyy-mm-dd");
 
    Date Dateafter = Getdayafter (new date (), "yyyy-mm-dd");
    System.out.println ("The Day Before": "+ predate +" after "The One": "+ nextdate");
 
    System.err.println ("The day Before:" +date2string (Befroday, "yyyy-mm-dd") + "//After the following days:" + dateafter);
    String mat = "YYYY-MM-DD";//This is best read from the database SimpleDateFormat DateTimeFormat = new SimpleDateFormat (MAT);
    Date DD = Dateutil.getdaybefore (new date (), mat); Date Befroday = Getdaybefore (new date (), yyyy-mm-dd ");
    String sd = date2string (Befroday, "yyyy-mm-dd");
    String datestr = Datetimeformat.format (Befroday);
 
    System.out.println ("datestr=" +datestr+ "SD" +SD);
    list<date> listdate = get2datelistdate ("2016-05-01", "2016-05-03", "yyyy-mm-dd");
    list<string> listdatestr = get2datelistdatestr ("2016-05-01", "2016-05-03", "yyyy-mm-dd");
    Date StartDate = new Date ();
    Date EndDate = Datetimeformat.parse ("2016-05-31");
     
    list<date> listdate = get2datelistwithdate (startdate,enddate, "yyyy-mm-dd");
    for (int i = 0; i < listdate.size (); i++) {System.err.println (Listdate.get (i));
    /*for (int i = 0; i < listdatestr.size (); i++) {//system.out.println (Listdatestr.get (i)); }*/
 
  }

The above is the entire content of this article, I hope to learn Java program to help you.

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.