Public final static String PATTERN = "YYYY-MM-DD";
Public final static String pattern_time = "Yyyy-mm-dd HH:mm:ss";
public static date parsestring (string date, string format)
Throws ParseException {
SimpleDateFormat DateFormat =
New SimpleDateFormat (format);
Date timestamp = Dateformat.parse (date);
return timestamp;
}
public static string Parsedate (date date, string format) {
if (date = = null) {
return null;
}
SimpleDateFormat DateFormat = new SimpleDateFormat (format);
String timestamp = Dateformat.format (date);
return timestamp;
}
public static string Paypremiumtoyuan (String paypremium) {
BigDecimal BDL = bigdecimal.valueof (integer.valueof (Paypremium));
String result = string.valueof (Bdl.multiply (bigdecimal.valueof (0.01)));
return result;
}
/**
* Get the first day of the week
*
* @return
*/
public static String Getfirstdayofweek () {
Calendar C = calendar.getinstance (Locale.china);
C.setfirstdayofweek (Calendar.monday);
C.settimeinmillis (System.currenttimemillis ());
C.set (Calendar.day_of_week, calendar.monday);
return new SimpleDateFormat (PATTERN). Format (C.gettime ());
}
/**
* Get the last day of the week
*
* @return
*/
public static String Getlastdayofweek () {
String FirstDayOfWeek = Getfirstdayofweek ();
try {
Date date = ParseString (FirstDayOfWeek, PATTERN);
Calendar C = calendar.getinstance ();
C.settime (date);
C.add (Calendar.day_of_month, 6);
return new SimpleDateFormat (PATTERN). Format (C.gettime ());
} catch (ParseException e) {
E.printstacktrace ();
}
Return "";
}
/**
* Get the first day of the month
*
* @return
*/
public static String Getfirstdayofmonth () {
Calendar C = calendar.getinstance (Locale.china);
C.set (Calendar.day_of_month, 1);
return new SimpleDateFormat (PATTERN). Format (C.gettime ());
}
/**
* Get the last day of the month
*
* @return
*/
public static String Getlastdayofmonth () {
Calendar C = calendar.getinstance (Locale.china);
C.set (Calendar.day_of_month, C.getactualmaximum (Calendar.day_of_month));
return new SimpleDateFormat (PATTERN). Format (C.gettime ());
}
Test the above method:
public static void Main (string[] args) {
Calendar C1 = Calendar.getinstance ();
c1.set (C1.get (calendar.year), C1.get (Calendar.month), C1.get (Calendar.day_of_month), 0, 0, 0); Calendar C2 = calendar.getinstance (); c2.set (C2.get (calendar.year), C2.get (Calendar.month), C2.get (Calendar.day_of_month), 0, 0, 0);
try {Date begindate = dateutil.parsestring ("2016-04-10", Dateutil.pattern); Date endDate = dateutil.parsestring ("2016-04-14", Dateutil.pattern); Calendar C = calendar.getinstance (); C.settime (begindate); Long stepcount = 0L; while (Begindate.before (endDate)) {String Userstepcount = "200"; if (! Stringutils.isempty (Userstepcount)) {Stepcount + = Long.parselong (Userstepcount); } c.add (Calendar.day_of_month, 1); Begindate = C.gettime (); }} catch (NumberFormatException e) {e.printstacktrace (); } catch (ParseException e) {e.printstacktrace (); } }
========================================================
/**
* Convert date string to date
* @param date
* @param format
* @return
*/
public static Date parsedate (String date,string format) {
if (date==null) {
return null;
}
SimpleDateFormat SDF = new SimpleDateFormat (format);
try{
return Sdf.parse (date);
}catch (ParseException e) {
return null;
}
}
/**
* Convert date string to date
* @param date
* @return
*/
public static Date parsedate (String date) {
if (date!=null) {
date = Date.replaceall ("\\d", "" ");
return parsedate (date, "YyyyMMdd");
}
return null;
}
/**
* Convert date to specified format string
* @param date
* @param format
* @ Return
*/
public static String formatdate (Date date,string format) {
if (date==null) {
return "";
}
try{
simpledateformat sdf = new SimpleDateFormat (format);
return Sdf.format (date);
}catch (illegalargumentexception e) {
return "";
}
}
/**
* Converts a date string to the specified format
* @param date
* @param Srcformat
* @param Destformat
* @return
*/
public static string FormatDate (String date,string srcformat,string Destformat) {
if (date==null) {
Return "";
}
Date time = Parsedate (Date,srcformat);
return FormatDate (Time,destformat);
}
/**
* Converts a date string to the specified format
* @param date
* @param format
* @return
*/
public static string FormatDate (string date,string format) {
if (date==null) {
Return "";
}
Date = Date.replaceall ("\\d", "" ");
return FormatDate (date, "YYYYMMDD", format);
}
/**
* Add and Subtract date fields
* @param date
* @param addnum
* @param field with calendar add parameter
* @return
*/
public static date Adddatefield (date date,int addnum, int field) {
Calendar calendar = Calendar.getinstance ();
Calendar.settime (date);
Calendar.add (field, addnum);
return Calendar.gettime ();
}
/**
* Set Date field
* @param date
* @param value
* @param field with calendar set parameter
* @return
*/
public static date Setdatefield (date date,int value, int field) {
Calendar calendar = Calendar.getinstance ();
Calendar.settime (date);
Calendar.set (field, value);
return Calendar.gettime ();
}
/**
* Get the last day of the month of the given date
* @param date
* @return
*/
public static date Getlastdayofmonth (date date) {
Date = Adddatefield (date,1, calendar.month);
Date = Setdatefield (date,1, calendar.day_of_month);
Date = Adddatefield (date,-1, calendar.day_of_year);
return date;
}
/**
* Returns the date value that is partially truncated in the specified element format
* @param date
* @param field
* @return
*/
public static date truncdate (date Date,int field) {
Calendar calendar = Calendar.getinstance ();
Calendar.settime (date);
if (Calendar.millisecond>=field)
Date = Setdatefield (date,0, Calendar.millisecond);
if (Calendar.second>=field)
Date = Setdatefield (date,0, Calendar.second);
if (Calendar.minute>=field)
Date = Setdatefield (date,0, Calendar.minute);
if (Calendar.hour_of_day>=field)
Date = Setdatefield (date,0, calendar.hour_of_day);
if (Calendar.day_of_week==field)
Date = Setdatefield (Date,calendar.sunday, Calendar.day_of_week);
if (Calendar.day_of_month==field)
Date = Setdatefield (date,1, calendar.day_of_month);
if (Calendar.day_of_year==field)
Date = Setdatefield (date,1, calendar.day_of_year);
return date;
}
/**
*
* @param begindate
* @param endDate
* @return
*/
public static long Comparetowdate (String begindate,string endDate) {
Return Comparetowdate (Parsedate (begindate), Parsedate (endDate));
}
/**
*
* @param begindate
* @param endDate
* @return
*/
public static long Comparetowdate (Date begindate,date endDate) {
Long beginTime = Begindate==null? Long.MAX_VALUE:beginDate.getTime ();
Long endTime = Enddate==null? Long.MAX_VALUE:endDate.getTime ();
return endtime-begintime;
}
/**
* Does the time interval overlap
* @param beginDate1 time period 1 start time
* @param endDate1 time period 1 end time
* @param beginDate2 time period 2 start time
* @param endDate2 time period 2 end time
* @return Returns True when there is no overlap, the overlap returns false
*/
public static Boolean Checkdaterangesconstraint (Date begindate1,date enddate1,date begindate2,date endDate2) {
Boolean checked = true;
Long begindate1time = Begindate1==null? Long.MIN_VALUE:beginDate1.getTime ();
Long begindate2time = Begindate2==null? Long.MIN_VALUE:beginDate2.getTime ();
Long enddate1time = Enddate1==null? Long.MAX_VALUE:endDate1.getTime ();
Long enddate2time = Enddate2==null? Long.MAX_VALUE:endDate2.getTime ();
Cross the situation
if (begindate1time<=begindate2time&&enddate1time>begindate2time) {
Checked = false;
}
Cross the situation
if (begindate1time<enddate2time&&enddate1time>=enddate2time) {
Checked = false;
}
Inclusion of the case
if (Checkdaterangesinclude (Begindate1,enddate1,begindate2,enddate2)) {
Checked = false;
}
Inclusion of the case
if (Checkdaterangesinclude (begindate2,enddate2,begindate1,enddate1)) {
Checked = false;
}
return checked;
}
/**
* Does the time interval overlap
* @param beginDate1 time period 1 start time
* @param endDate1 time period 1 end time
* @param beginDate2 time period 2 start time
* @param endDate2 time period 2 end time
* @return Returns True when there is no overlap, the overlap returns false
*/
public static Boolean Checkdaterangesconstraint (String begindate1,string enddate1,string begindate2,string endDate2) {
Return Checkdaterangesconstraint (Parsedate (beginDate1), Parsedate (endDate1), Parsedate (BeginDate2), Parsedate ( ENDDATE2));
}
/**
* Whether the time interval is within another time period
* @param start time of begininnerdate small time period
* @param endinnerdate The end time of the small time period
* @param start time of beginoutdate large time period
* @param the end time of the Endoutdate large time period
* @return returns True when a relationship is included, otherwise false
*/
public static Boolean checkdaterangesinclude (Date begininnerdate,date endinnerdate,date beginoutdate,date endOutDate) {
Boolean checked = true;
Long begininnertime = Begininnerdate==null? Long.MIN_VALUE:beginInnerDate.getTime ();
Long beginouttime = Beginoutdate==null? Long.MIN_VALUE:beginOutDate.getTime ();
Long endinnertime = Endinnerdate==null? Long.MAX_VALUE:endInnerDate.getTime ();
Long endouttime = Endoutdate==null? Long.MAX_VALUE:endOutDate.getTime ();
if (begininnertime<beginouttime) {
Checked = false;
}
if (endinnertime>endouttime) {
Checked = false;
}
return checked;
}
/**
* Whether the time interval is within another time period
* @param start time of begininnerdate small time period
* @param endinnerdate The end time of the small time period
* @param start time of beginoutdate large time period
* @param the end time of the Endoutdate large time period
* @return returns True when a relationship is included, otherwise false
*/
public static Boolean checkdaterangesinclude (String begininnerdate,string endinnerdate,string beginoutdate,string Endoutdate) {
Return Checkdaterangesinclude (Parsedate (begininnerdate), Parsedate (endinnerdate), Parsedate (BeginOutDate), Parsedate (endoutdate));
}
/**
* Whether the time interval has intersection
* @param beginDate1 time period 1 start time
* @param endDate1 time period 1 end time
* @param beginDate2 time period 2 start time
* @param endDate2 time period 2 end time
* @return Returns True when there is a intersection, otherwise false
*/
public static Boolean checkdaterangesmixed (Date begindate1,date enddate1,date begindate2,date endDate2) {
Boolean checked = true;
Long beginTime1 = Begindate1==null? Long.MIN_VALUE:beginDate1.getTime ();
Long beginTime2 = Begindate2==null? Long.MIN_VALUE:beginDate2.getTime ();
Long endTime1 = Enddate1==null? Long.MAX_VALUE:endDate1.getTime ();
Long endTime2 = Enddate2==null? Long.MAX_VALUE:endDate2.getTime ();
if (begintime1>endtime2) {
Checked = false;
}
if (begintime2>endtime1) {
Checked = false;
}
if (begintime1==endtime2) {
if (endtime2!=begintime2)
Checked = false;
}
if (begintime2==endtime1) {
if (endtime1!=begintime1)
Checked = false;
}
return checked;
}
/**
* Whether the time interval has intersection
* @param beginDate1 time period 1 start time
* @param endDate1 time period 1 end time
* @param beginDate2 time period 2 start time
* @param endDate2 time period 2 end time
* @return Returns True when there is a intersection, otherwise false
*/
public static Boolean checkdaterangesmixed (String begindate1,string enddate1,string begindate2,string endDate2) {
Return checkdaterangesmixed (Parsedate (beginDate1), Parsedate (endDate1), Parsedate (BeginDate2), Parsedate (ENDDATE2) );
}
public static int calculateage (String birth) {
int age =-1;
Date BirthDay = parsedate (birth, "yyyy-mm-dd");
Date sysdate = truncdate (New Date (), calendar.hour_of_day);
while (Comparetowdate (birthday,sysdate) >0) {
Age +=1;
BirthDay = Adddatefield (birthday,1,calendar.year);
}
return age;
}
public static date Addmonth (date Date,int value) {
Calendar calendar = Calendar.getinstance ();
Calendar.settime (date);
int actualday = Calendar.get (calendar.day_of_month);
int actualmaxday = Calendar.getactualmaximum (calendar.day_of_month);
Calendar.add (calendar.month, value);
if (Actualday ==actualmaxday) {
Calendar.set (Calendar.day_of_month, actualmaxday);
}
return Calendar.gettime ();
}
Test the above method:
public static void Main (string[] args) {Calendar Newcalendar = calendar.getinstance (); Newcalendar.set (1999, 1, 28); System.out.println (Newcalendar.getmaximum (calendar.day_of_month)); System.out.println (Newcalendar.getactualmaximum (calendar.day_of_month)); System.out.println (Newcalendar.getactualminimum (calendar.day_of_month)); System.out.println (Newcalendar.getgreatestminimum (calendar.day_of_month)); System.out.println (Newcalendar.getleastmaximum (calendar.day_of_month));
}
Date format Digital Tool class: Date format cross-transfer