Java on time (date) essay!

Source: Internet
Author: User
Tags date now

/**
* Get System Current time
* @return System Current Time
*/
public static Date now () {
return new Date ();
}

/**
* Based on the date specified, get its relative days before date
* eg:data=2013-09-09
* days = 3
* Return to =2013-09-06
* Dates specified @param date
* @param days relative to day
* @return Date
*/
public static date Getdatebefore (date date, int days) {
if (date = = null)
Date = Now ();
Calendar now = Calendar.getinstance ();
Now.settime (date);
Now.set (Calendar.date, Now.get (calendar.date)-days);
return Now.gettime ();
}

/**
* Gets the date relative to the number of months months, based on the specified date
* eg:data=2013-09-09
* Month = 1
* Return to =2013-08-10
* Dates specified @param date
* @param months relative number of months
* @return Date
*/
public static date Getdatebeforemonth (date date, int months) {
if (date = = null)
Date = Now ();
Calendar now = Calendar.getinstance ();
Now.settime (date);
Now.set (Calendar.month, Now.get (calendar.month)-months);
return Now.gettime ();
}

/**
* The date after which the relative number of months is months based on the specified date
* eg:data=2013-09-09
* Month = 1
* Return to =2013-10-10
* Dates specified @param date
* @param months relative number of months
* @return Date
*/
public static date Getdateaftermonth (date date, int months) {
if (date = = null)
Date = Now ();
Calendar now = Calendar.getinstance ();
Now.settime (date);
Now.set (Calendar.month, Now.get (calendar.month) + months);
return Now.gettime ();
}

/**
* Based on the date specified, get its date after days
* eg:data=2013-09-09
* days = 3
* Return to =2013-09-12
* Dates specified @param date
* @param days relative to day
* @return Date
*/
public static date Getdateafter (date date, int days) {
if (date = = null)
Date = Now ();
Calendar now = Calendar.getinstance ();
Now.settime (date);
Now.set (Calendar.date, Now.get (calendar.date) + days);
return Now.gettime ();
}

/**
* Based on the specified date, the date after which the relative number of years years is obtained
* eg:data=2013-09-09
* Month = 1
* Return to =2014-09-08
* Dates specified @param date
* @param year relative number of years
* @return Date
*/
public static date Getdateafteryear (date date, int year) {
if (date = = null)
Date = Now ();
Calendar now = Calendar.getinstance ();
Now.settime (date);
Now.set (Calendar.year, Now.get (calendar.year) + year);
return Now.gettime ();
}

public static string GetDate (date date, string format) {
if (date = = null)
Date = Now ();
return new SimpleDateFormat (format). format (date);
}

public static string Getcurrentdate (string format) {
Return GetDate (New Date (), format);
}

public static String Getcurrentdate () {
Return Getcurrentdate ("Yyyy-mm-dd");
}

/**
* Custom formatting time
*
* @param date Time
* @param formatstr formatted string, default MM/DD/YYYY
* @return formatted time string
*/
public static string formartdate (date date, string formatstr) {
if (date = = null)
Date = Now ();
if (Stringutil.isempty (FORMATSTR)) {
Formatstr = "mm/dd/yyyy";
}
SimpleDateFormat sdf = new SimpleDateFormat (FORMATSTR);
return Sdf.format (date);
}

/**
* Convert a time format character to a time type
* @param datestr string with time format
* @param pattern format string
* @return Post-conversion time type data
*/
public static Date formdate (String datestr,string pattern) {
SimpleDateFormat SDF = new SimpleDateFormat (pattern);
Date date = null;
try {
Date = Sdf.parse (DATESTR);
} catch (ParseException e) {
E.printstacktrace ();
}
return date;
}

/**
* Get start time of the day
*
* @return
*/
public static Date GetStartTime () {
Calendar Todaystart = Calendar.getinstance ();
Todaystart.set (calendar.hour, 0);
Todaystart.set (calendar.minute, 0);
Todaystart.set (Calendar.second, 0);
Todaystart.set (Calendar.millisecond, 0);

return Todaystart.gettime ();
}

/**
* Get the day end time
*
* @return
*/
public static Date Getendtime () {
Calendar todayend = Calendar.getinstance ();
Todayend.set (Calendar.year, 4028);
Todayend.set (Calendar.hour_of_day, 23);
Todayend.set (Calendar.minute, 59);
Todayend.set (Calendar.second, 59);
Todayend.set (Calendar.millisecond, 999);
return Todayend.gettime ();
}

/**
* Comparison of two time difference
* @param start time
* @param end time
* @return Two time difference
* @throws ParseException
*/

public static long Timecompare (Date start,date end) {
Calendar Acalendar = Calendar.getinstance ();

Acalendar.settime (start);

int day1 = Acalendar.get (calendar.day_of_year);

Acalendar.settime (end);

int day2 = Acalendar.get (calendar.day_of_year);

return day2-day1;

}

/**
* Comparison of two time sizes
* @param start
* @param end
* @return End-start > 0 true, otherwise false
*/
public static Boolean compare (Date Start,date end) {
Calendar Acalendar = Calendar.getinstance ();

Acalendar.settime (start);

int day1 = Acalendar.get (calendar.day_of_year);

Acalendar.settime (end);

int day2 = Acalendar.get (calendar.day_of_year);

Return day2-day1 >= 0? True:false;

}

public static int GetYear () {
Calendar a=calendar.getinstance ();
Return A.get (calendar.year);
}
public static int GetMonth () {
Calendar a=calendar.getinstance ();
Return A.get (calendar.month) +1;
}
public static int GetDate () {
Calendar a=calendar.getinstance ();
Return A.get (calendar.date);
}



/**
* Get the first day of the year
* @param year
* @return
*/
public static Date Getyearfirst () {
Calendar ca=calendar.getinstance ();
int year = Ca.get (calendar.year);

Ca.clear ();
Ca.set (Calendar.year, year);
Date Yearfirst = Ca.gettime ();
return yearfirst;
}


/**
* Get the last day of the year
* @param year
* @return
*/
public static Date Getyearlast () {
Calendar ca=calendar.getinstance ();
int year = Ca.get (calendar.year);

Ca.clear ();
Ca.set (Calendar.year, year);
Ca.roll (Calendar.day_of_year,-1);

Date yearlast = Ca.gettime ();

return yearlast;

}


public static void Main (string[] args) {
String str1 = "2014-02-20";
String str2 = "2014-12-20";
list<string> Listb = new arraylist<string> ();
list<string> Liste = new arraylist<string> ();
Listb.add ("2014-01-01");
Liste.add ("2014-12-30");

System.out.println (Insertvalidate (str1, str2, Listb, Liste));

}

/**
* Judgment Time Cross
* @param begindate
* @param endDate
* @param begindatelist
* @param enddatelist
* @return False Cross, true, not cross.
*/
public static Boolean insertvalidate (String begindate,string enddate,list<string> begindatelist,list<string > Enddatelist) {
Integer begin = Trimchar (begindate);
Integer end = Trimchar (endDate);
list<integer> beginList = new arraylist<integer> ();
list<integer> endlist = new arraylist<integer> ();
for (int i = 0; i < begindatelist.size (); i++) {
Beginlist.add (Trimchar (Begindatelist.get (i)));
Endlist.add (Trimchar (Enddatelist.get (i)));//A start time corresponds to an end time
}

for (int i = 0; i < endlist.size (); i + +) {
if (Begin < Endlist.get (i) && end > Beginlist.get (i)) {
return false; Overlap exists
}
}

return true;
}


public static Integer Trimchar (String str) {
String year = str.substring (0,4);
String Yue = str.substring (5,7);
String ri = str.substring (8,10);
String newstring = Year+yue+ri;
System.out.println (newstring);
Return Integer.parseint (newstring);
}

Java on time (date) essay!

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.