Various date acquisition

Source: Internet
Author: User
Tags date now dateformat getdate
Get a date of n days
public static Date getTime (int day) {
    Calendar objcalendar=calendar.getinstance ();
    Objcalendar.add (calendar.date,day);
    Date objdate=objcalendar.gettime ();
    System.out.print (objdate);
    return objdate;
}
/**
* Get the time n days ago
* @param Day
* @return
*/
public static Date getbeforedays (int day) {
    Calendar cal=calendar.getinstance ();
    int inputdayofyear = Cal.get (calendar.day_of_year);
    Cal.set (Calendar.day_of_year, inputdayofyear-day);
    Date d =cal.gettime ();
    return D;
}
/**

* Convert date format string to date type

* Dates @param date String type

*/

public static date Dateformat2 (String date) throws ParseException {
    SimpleDateFormat f = new SimpleDateFormat ("Yyyy-m M-dd HH:mm:ss ");
    Date Mydate=f.parse (date);
    return mydate;
}
/ **

* The string date of the specified type method is converted to date

*/

public static date format (string Date, String pattern) {
    if (pattern = = NULL | | pattern.equals ("") | | Pattern.equals ("null")) {Pattern
        = "Yyyy-mm-dd HH:mm:ss";
    }
    If date = null | | date.equals ("") | | Date.equals ("null")) {return
        new Date ();
    }
    Date d = null;
    try {
        d = new Java.text.SimpleDateFormat (pattern). Parse (date);
    \ catch (parseexception pe) {
    }
    return D;
}
/**

* Get the date parameter after the specified month (before) to send negative numbers
* @param the time month want
* @param datestr date format must be YYYY-MM-DD HH:MM:SS
* @return Yyyy-mm-dd HH:mm:ss time
*/

public static String  getaftermonth (int month,string datestr) {
        Calendar c = calendar.getinstance ();//Get an instance of a calendar   
        SimpleDateFormat sdf = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");   
        Date date = null;   
        try{Date   
            = Sdf.parse (DATESTR);//Initial date   
        }catch (Exception e) {  


        }   
        c.settime (date);//Set Calendar time   
        C.add ( Calendar.month,month)//Add 6 months to the calendar month   
        String strdate = Sdf.format (C.gettime ()); To the date you want 6 months back.   
        strdate;
 }
/**

* Get the current time H hours after
* @param m
* @return
*/
@SuppressWarnings ("Static-access")

public static string getcalendartime (int h) {
    String str = "";
    Calendar cl = calendar.getinstance ();
    Cl.add (CL. Hour_of_day, h);
    str = Cl.get (CL. Year) + "years" + (Cl.get (CL. MONTH + 1) + "month"
          + (Cl.get) (CL. Day_of_month) + "Day" + (Cl.get) (CL. Hour_of_day)
          + "Time" + (Cl.get) (CL. MINUTE) + "min" + (Cl.get) (CL. SECOND)) + "seconds";
    return str;
}
/**
* YYYY year MM month DD Day week
* @return
*/
@SuppressWarnings ("deprecation")
public static String GetDate () {
    StringBuffer sb = new StringBuffer ();
    Date d = new Date ();
    int hours = D.gethours ();
    int minute = D.getminutes ();
    String[] weekdays = {"Day", "one", "two", "three", "four", "five", "Six"};
    Sb.append ((D.getyear () + 1900));
    Sb.append ("year");
    Sb.append (D.getmonth () + 1);
    Sb.append ("month");
    Sb.append (D.getdate ());
    Sb.append ("Day Week");
    Sb.append (Weekdays[d.getday ());
    return sb.tostring ();
}
/**

* Returns the number of days difference between two dates
* @param d1 A long time
* @param D2 a short time
* @return int
*/

public static int Dispersionday (string strDate1, String strDate2) {int iday = 0;//record number of days to try {int in
        Dex1,index2;
        Index1=strdate1.indexof ('-');
        Index2=strdate1.lastindexof ('-');
        int nyear, nmonth, Nday;
        Nyear = Integer.parseint (strdate1.substring (0, index1));
        Nmonth = Integer.parseint (strdate1.substring (index1+1, index2));
        Nday = Integer.parseint (strdate1.substring (index2+1)); Calendar objCalendarDate1 = Calendar.getinstance ();//This function is new GregorianCalendar () Objcalendardate1.set (Nyear, n
               
        Month, Nday);
        Index1=strdate2.indexof ('-');
      
        Index2=strdate2.lastindexof ('-');
        Nyear = Integer.parseint (strdate2.substring (0, index1));
        Nmonth = Integer.parseint (strdate2.substring (index1+1, index2));
        Nday = Integer.parseint (strdate2.substring (index2+1));
        Calendar ObjCalendarDate2 = Calendar.getinstance (); Objcalendardate2.set (Nyear, Nmonth, Nday);
        if (Objcalendardate2.equals (objCalendarDate1)) return 0; if (Objcalendardate2.after (objCalendarDate1)) {//if strdate2>strdate1 while!objcalendardate2.equals (OBJC
                alendarDate1)) {Objcalendardate1.add (objcalendardate1.date, 1);
            iday++; 
        The Iday =-iday;//minus sign represents a few days apart, plus the positive symbol has been in the past few days ... else{//if Strdate1>strdate2 while (!objcalendardate2.equals (objCalendarDate1)) {objcal
                Endardate2.add (objcalendardate2.date, 1);
            iday++;
The catch (Exception e) {} return iday; }
Determine whether the time is within a certain period

public static Boolean Istimein (Date time,date begin,date end) {return
       time.gettime () >=begin.gettime () & & Time.gettime () <=end.gettime ();
}
Get the date of this Monday
public static Date Getmondayofweek () {
    int mondayplus = Getmondayplus ();
    GregorianCalendar currentdate = new GregorianCalendar ();
    Currentdate.add (Gregoriancalendar.date, mondayplus);
    Date Monday = Currentdate.gettime ();
return Monday;

}
Get the date of this week Sunday
<pre name= "code" class= "java" >public static  Date getcurrentweekday () {
    int mondayplus = Getmondayplus ();
    GregorianCalendar currentdate = new GregorianCalendar ();
    Currentdate.add (Gregoriancalendar.date, mondayplus+6);
    Date Monday = Currentdate.gettime ();
    return Monday;
}

Get the date of last Monday
public static  Date Getpreviousweekday () {
    int mondayplus =getmondayplus ();
    GregorianCalendar currentdate = new GregorianCalendar ();
    Currentdate.add (gregoriancalendar.date, Mondayplus + 7 *-1);
    Date Monday = Currentdate.gettime ();
    DateFormat df = dateformat.getdateinstance ();
    String Premonday = Df.format (Monday);
    return Monday;
}
Get the date of last Sunday
public static  Date Getpreviousweeksunday () {
//weeks--;
int mondayplus = Getmondayplus ();
GregorianCalendar currentdate = new GregorianCalendar ();
Currentdate.add (Gregoriancalendar.date, mondayPlus-1);
Date Monday = Currentdate.gettime ();
DateFormat df = dateformat.getdateinstance ();
String Premonday = Df.format (Monday);
return Monday;
}
Get the first day of the month
public static string Getfirstdayofmonth () {
string str = "";
SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd");
Calendar lastdate = Calendar.getinstance ();
Lastdate.set (calendar.date,1);//set to the current month, number 1th
Str=sdf.format (Lastdate.gettime ());
return str;
}

Calculates the last day of the month, returns a string
public static string Getdefaultday () {
string str = "";
SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd");
Calendar lastdate = Calendar.getinstance ();
Lastdate.set (calendar.date,1)//set to the current month, number 1th
Lastdate.add (calendar.month,1),//plus one months, into the next month 1th
Lastdate.add ( CALENDAR.DATE,-1);/Minus one day, into the last day of the month
Str=sdf.format (Lastdate.gettime ());
return str;
}

Get Day time
public static string Getnowtime (string dateformat) {The
    date now = new Date ();
    SimpleDateFormat DateFormat = new SimpleDateFormat (DateFormat);//You can easily modify the date format
    String hehe = Dateformat.format (now) ;
    return hehe;
}

Get the number of days that the current date differs from this Sunday
public static int Getmondayplus () {
    Calendar cd = Calendar.getinstance ();
    Get today is the day of the week, Sunday is the first, Tuesday is the next day
    ... int dayofweek = Cd.get (Calendar.day_of_week)-1;         Because press China Monday as the first day so here minus 1
    if (DayOfWeek = = 1) {return
        0;
    } else {return
        1-dayofweek;}
}


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.