Date plus minus

Source: Internet
Author: User



Package cn.com.css.misps.graph.util;

Import Java.text.SimpleDateFormat;
Import Java.util.Calendar;
Import Java.util.Date;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;

/**
* @brief Dateutil.java Date Tool class
* @attention
* @author coating rights
* @date 2013-10-13
* @note begin modify by null
*/
public class Dateutil {

 /**
  * \brief generated similar: 201310181533 time value
  *
  * @return
  * @attention
  * @author paint Rights
  * @date 2013-10-18
  * @note begin modify by NULL
  */
 public static Stri Ng Gettimenow () {
  //gets a calendar using the default time zone and locale
  calendar calendar = calendar.getinstance ();
  //
  string year = Calendar.get (calendar.year) + "";
  //month
  string month = ((Calendar.get (calendar.month) + 1) < 10)? "0"
    + (Calendar.get (calendar.month) + 1): ""
    + (Calendar.get ( Calendar.month) + 1);
  //today
  string day = (Calendar.get (calendar.day_of_month) < 10)? "0"
    + calendar.get (calendar.day_of_month): ""
    + Calendar.get (calendar.day_of_month);

Time, 24-hour system
String hour = (Calendar.get (calendar.hour_of_day) < 10)? "0"
+ Calendar.get (calendar.hour_of_day): ""
+ Calendar.get (calendar.hour_of_day);
Score of
String minute = (Calendar.get (Calendar.minute) < 10)? "0"
+ Calendar.get (Calendar.minute): ""
+ Calendar.get (calendar.minute);
Seconds, not for the time being
int seconds = Calendar.get (Calendar.second);
Return year + month + day + hour + minute;
}

/**
* \brief Gets the date of n days
*
* @param string
* @param string2
* @attention
* @author coating rights
* @date 2013-11-16
* @note begin modify by null
*/
public static string Handleadd (String str, Integer num) {
Get a default time zone and locale to get a calendar
Calendar calendar = Calendar.getinstance ();
String newstr = null;
if (Str.tolowercase (). StartsWith ("Y")) {
Newstr = (Calendar.get (calendar.year) + num) + "";
There are several y in the judging character, if there is only one, the last two bits are obtained, and if there are 4 bits, 4 bits are returned.
Return newstr.substring (Newstr.length ()-str.length ());
} else if (Str.startswith ("M")) {
Reset Month
Calendar.set (Calendar.month, Calendar.get (calendar.month) + num);
Otherwise, return directly
Return ((Calendar.get (calendar.month) + 1) < 10)? "0"
+ (Calendar.get (calendar.month) + 1): (Calendar
. Get (Calendar.month) + 1)
+ "";
} else if (Str.startswith ("D")) {
int monthNow1 = Calendar.get (calendar.month) + 1;//get month

Calendar.set (Calendar.day_of_month, Calendar.get (calendar.day_of_month) + num);
int monthNow2 = Calendar.get (calendar.month) + 1;//get month
String mouth = ((Calendar.get (calendar.month) + 1) < 10)? "0"
+ (Calendar.get (calendar.month) + 1) + "month": (Calendar
. Get (Calendar.month) + 1)
+ "Month";

Return (Calendar.get (Calendar.day_of_month) < 10)? Mouth + "0"
+ Calendar.get (calendar.day_of_month): Mouth
+ Calendar.get (calendar.day_of_month);
} else if (Str.startswith ("H")) {
Calendar.set (Calendar.hour_of_day, Calendar
. Get (Calendar.hour_of_day)
+ num);
Return (Calendar.get (Calendar.hour_of_day) < 10)? "0"
+ Calendar.get (calendar.hour_of_day): ""
+ Calendar.get (calendar.hour_of_day);
} else if (Str.startswith ("M")) {
Calendar.set (Calendar.minute, Calendar.get (calendar.minute) + num);
Return (Calendar.get (Calendar.minute) < 10)? "0"
+ Calendar.get (calendar.minute): Calendar
. Get (Calendar.minute)
+ "";
} else if (Str.startswith ("s")) {
Calendar.set (Calendar.second, Calendar.get (calendar.second) + num);
Return (Calendar.get (Calendar.second) < 10)? "0"
+ Calendar.get (calendar.second): Calendar
. Get (Calendar.second)
+ "";
} else {
Calendar.set (Calendar.millisecond, Calendar
. Get (Calendar.millisecond)
+ num);
Return Calendar.get (Calendar.millisecond) + "";
}
}

/**
* \brief Gets the date of n days
*
* @param string
* @param string2
* @attention
* @author coating rights
* @date 2013-11-16
* @note begin modify by null
*/
public static string Handleminus (String str, Integer num) {
Get a default time zone and locale to get a calendar
Calendar calendar = Calendar.getinstance ();
String newstr = null;
if (Str.tolowercase (). StartsWith ("Y")) {
Newstr = (Calendar.get (calendar.year)-num) + "";
There are several y in the judging character, if there is only one, the last two bits are obtained, and if there are 4 bits, 4 bits are returned.
Return newstr.substring (Newstr.length ()-str.length ());
} else if (Str.startswith ("M")) {
Reset Month
Calendar.set (Calendar.month, Calendar.get (calendar.month)-num);
Otherwise, return directly
Return ((Calendar.get (calendar.month) + 1) < 10)? "0"
+ (Calendar.get (calendar.month) + 1): (Calendar
. Get (Calendar.month) + 1)
+ "";
} else if (Str.startswith ("D")) {
Calendar.set (Calendar.day_of_month, Calendar
. Get (Calendar.day_of_month)
-num);
Return (Calendar.get (Calendar.day_of_month) < 10)? "0"
+ Calendar.get (calendar.day_of_month): Calendar
. Get (Calendar.day_of_month)
+ "";
} else if (Str.startswith ("H")) {
Calendar.set (Calendar.hour_of_day, Calendar
. Get (Calendar.hour_of_day)
-num);
Return (Calendar.get (Calendar.hour_of_day) < 10)? "0"
+ Calendar.get (calendar.hour_of_day): ""
+ Calendar.get (calendar.hour_of_day);
} else if (Str.startswith ("M")) {
Calendar.set (Calendar.minute, Calendar.get (calendar.minute)-num);
Return (Calendar.get (Calendar.minute) < 10)? "0"
+ Calendar.get (calendar.minute): Calendar
. Get (Calendar.minute)
+ "";
} else if (Str.startswith ("s")) {
Calendar.set (Calendar.second, Calendar.get (calendar.second)-num);
Return (Calendar.get (Calendar.second) < 10)? "0"
+ Calendar.get (calendar.second): Calendar
. Get (Calendar.second)
+ "";
} else {
Calendar.set (Calendar.millisecond, Calendar
. Get (Calendar.millisecond)
-num);
Return Calendar.get (Calendar.millisecond) + "";
}
}

/**
* \brief the current log output string in the specified format
*
* @param amask
* @return
* @attention This is just a conversion of the current log
* @author Toto
* @date 2013-11-15
* @note begin modify by null
*/
public static string Date2string (String amask) {
StringBuffer sb = new StringBuffer (amask);
if (Amask.contains ("[") && Amask.contains ("]")) {
Remove the string from the [] and replace it with the desired string.
Pattern pattern = Pattern.compile ("(\\[\\w*\\+{0,}\\-{0,}\\d{0,}\\)");
Get Matcher
Matcher Matcher = Pattern.matcher (sb.tostring ());

while (Matcher.find ()) {
String submask = matcher.group (0);
System.out.println ("Submask =" + Submask);

Then determine if there is a "+" sign in the string. If there is any further processing
if (Submask.contains ("+")) {
String Tempsubmask = Submask.replace ("[", "" "). Replace ("] ",
"");
string[] temp = tempsubmask.split ("\\+");
String replacedstring = Handleadd (temp[0], Integer
. parseint (Temp[1]));
SB = new StringBuffer (sb.tostring (). replace (Submask,
replacedstring));
Continue
}
if (Submask.contains ("-")) {
String Tempsubmask = Submask.replace ("[", "" "). Replace ("] ",
"");
string[] temp = tempsubmask.split ("\\-");
String replacedstring = Handleminus (temp[0], Integer
. parseint (Temp[1]));
SB = new StringBuffer (sb.tostring (). replace (Submask,
replacedstring));
Continue
}

Judgment, if there is a "[" or "]" the character is to be dealt with separately
SimpleDateFormat df = new SimpleDateFormat (submask);
String replacedstring = Df.format (New Date ());

Make a string like [yyyy] in a string into a string of 2013.
SB = new StringBuffer (sb.tostring (). replace (Submask,
replacedstring));
}
Return sb.tostring (). replace ("[", ""). Replace ("]", "" ");
} else {
return amask;
}
}

 /**
  * \brief to determine if there is a specific letter in the string
  *
  * @param args
  * @attention
  * @aut Hor Toto
  * @date 2013-11-15
  * @note begin modify by modifier modification Time Modified Content Summary Description
  */
 //public S tatic void Main (string[] args) {
  //System.out.println ("YYYY". toLowerCase ());
  //System.out.println (date2string ("sevp_nmc_weap_sob_er24_achn_lno_p9_[yy][mm][dd][hh][mm][fff]xx. MIC "));
  //System.out.println (date2string ("[YYYY]-[MM]-[DD] [HH]:[MM]:[SS] [E]");
  //System.out.println (date2string ("[yyyy].[ MM]. [DD] [G] ' at ' [HH]:[MM]:[SS] [z] "));
  //System.out.println (date2string ("[y][mm][dd][hh]_[mm][dd][hh]_[mm][dd].000"));
  //system.out.println (Date2string ("[y+1][mm][dd][hh]_[mm][dd][hh]_[mm][dd].000"));

Boolean B =
Pattern.compile ("(? i) [A-z]"). Matcher ("ada123456"). Find ();
System.out.println ("Do you want to include the specified characters?" + "+ b);
// //
Pattern pattern = pattern.compile ("yy");
//
SimpleDateFormat df = null;
Date chgdate = null;
try {
//
} catch (Exception e) {
Todo:handle exception
// }

System.out.println (date2string ("MM DD day 08 O'Clock-dd 08 O'Clock"));
System.out.println (Date2string ("[Yy][mm][dd][hh]00[mimi]"));
System.out.println (Date2string ("[y][mm][dd][hh]_[mm][dd][hh]_[mm][dd].000"));
System.out.println (date2string ("-----------------------------------------"));

String str =
"Sevp_nmc_weap_sob_er24_achn_lno_p9_[yy+1][mm][dd][hh][mm][fff]xx. MIC ";
Pattern pattern = Pattern.compile ("(\\[\\w*\\+{0,}\\d{0,}\\)");
Pattern pattern = Pattern.compile ("(\\[\\w*\\)");
//
Matcher Matcher = Pattern.matcher (str);
while (Matcher.find ()) {
SYSTEM.OUT.PRINTLN ("group 0:" + matcher.group (0));//Get Group No. 0-the entire match
System.out.println ("Group 1:" + matcher.group (1));//
Get the first set of matches--matching with (or)
System.out.println ("Group 2:" + Matcher.group (2));//
Get the second set of matches--with (ld!) Matched, the group is also the sub-expression
System.out.println ("Start 0:" + matcher.start (0) + "End 0:"
+ matcher.end (0));//Total matching index
System.out.println ("Start 1:" + matcher.start (1) + "End 1:"
+ matcher.end (1));//first set of matching indexes
System.out.println ("Start 2:" + Matcher.start (2) + "End 2:"
+ matcher.end (2));//second set of matching indexes
System.out.println (str.substring (Matcher.start (0),
Matcher.end (1)));//The substring--wor from the total match start index to the 1th set of matching end indexes
// }

Get the current Calendar class
Calendar calendar = Calendar.getinstance ();
Calendar.set (Calendar.year, calendar.year+1);
System.out.println (Calendar.get (calendar.year) +1);
Calendar.set (Calendar.month, Calendar.get (calendar.month) +3);
System.out.println (Calendar.get (calendar.month));

Calendar.set (Calendar.day_of_month, Calendar.get (calendar.day_of_month) + 3);
Calendar.set (Calendar.hour_of_day, Calendar.get (calendar.hour_of_day) + 3);
System.out.println (Calendar.get (calendar.hour_of_day));
//}
}

Date plus minus

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.