Frequently used dates in Java format (full)

Source: Internet
Author: User
Tags add days dateformat java format

Import Java.text.dateformat;import Java.text.parseexception;import Java.text.simpledateformat;import Java.util.calendar;import java.util.date;/** * Gets the current date format: Yyyy-mm-dd HH:mm:ss * * @author Arthur (user_006) * @version 1.0 .0 2010/04/24 14:00 (Saturday) */public class Timerutil {/** * Gets the current date, default format * * @return current date */public synchronized static Da Te Getcurrentcalendar () {Calendar calendar = calendar.getinstance (); return Calendar.gettime ();} /** * Gets the current date, format yyyy-mm-dd, such as: 2010-04-24 * * @return */public synchronized static String getcurrentdate () {Calendar CA Lendar = Calendar.getinstance (); StringBuffer sb = new StringBuffer (), Sb.append (Calendar.get (Calendar.year)). Append ("-"); Sb.append (Calendar.get ( Calendar.month) + 1). Append ("-"); Sb.append (Calendar.get (Calendar.day_of_month)); return sb.tostring ();}  /** * Converts the date of the character form to the type of date, the format yyyy-mm-dd * * @param the string form of date * Date * * @return data type Date */public synchronized Static Date Converttodate (String date) {DateFormat format = new simpleDateFormat ("Yyyy-mm-dd"); try {return format.parse (date);} catch (ParseException e) {e.printstacktrace ();} return null;}  /** * Convert the date to the specified format * * @param date * The format to be converted * @param pattern * converted formats * @return */public synchronized  Static date format (date date, String pattern) {if (pattern! = null && Pattern.trim (). Length () > 0) {dateformat format = new SimpleDateFormat (pattern); String stringdate = Format.format (date); return timerutil.converttodate (stringdate);} return null;}  /** * Convert date to string, format yyyy-mm-dd HH:mm:ss * * @param date * dates to convert * @return */public synchronized static string ConvertToString (date date) {DateFormat format = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); return Format.format (date) ;}  /** * Convert date to string, format yyyy-mm-dd HH:mm:ss * * @param date * dates to convert * @return */public synchronized static string Converttostrshort (date date) {DateFormat format = new SimpleDateFormat ("Yyyy-mm-dd"); return Format.format (date);} /** * Convert date to string, format yyYYMMDDHHMMSS * * @param date * dates to convert * @return */public synchronized static String convertToString2 (date date {DateFormat format = new SimpleDateFormat ("Yyyymmddhhmmss"); return Format.format (date);} /** * Convert a date to a string of the specified format * * @param date * The format to convert * @param formattext * converted formats * @return */public SYNCHR onized static String converttostring (Date date,string formattext) {dateformat format = new SimpleDateFormat (formattext); Calendar calendar = Calendar.getinstance (); StringBuffer sb = new StringBuffer (), Sb.append (Calendar.get (Calendar.year)). Append ("-"); Sb.append (Calendar.get ( Calendar.month) + 1). Append ("-"); Sb.append (Calendar.get (Calendar.day_of_month)); return Format.format (date);} /** * Get this Monday date * * @return */public synchronized static date Getmondayofweek () {Calendar calendar = calendar.getinstance ( ); Calendar.set (Calendar.day_of_week, calendar.monday);//This setting starts from Monday, if you need to take the initiative according to the system time zone, then return by the way below Calendar.gettime ();} /** * Get this Sunday day * * @return */public Synchronized static Date Getcurrentweekday () {Calendar calendar = calendar.getinstance (); Calendar.add (Calendar.date, Getcurrentplus ());//The date of the current date plus the number of days between the current date and this Sunday return Calendar.gettime ();} /** * Get last Sunday date * * @return */public synchronized static date Getpreviousweekday () {Calendar calendar = Calendar.getinstan CE ();//Calendar.set (Calendar.day_of_week, calendar.sunday+7); Calendar.add (Calendar.date, Getcurrentplus ()-7);// Return Calendar.gettime () with the date of the current date plus the number of days that the current date differs from this Sunday;} /** * Get last Sunday date * * @return */public synchronized static date Getpreviousmonday () {Calendar calendar = Calendar.getinstanc E ();//Calendar.set (Calendar.day_of_week, calendar.sunday+7); Calendar.add (Calendar.date, Getcurrentplus ()-13);// Return Calendar.gettime () with the date of the current date plus the number of days that the current date differs from this Sunday;} /** * Get last Sunday date * * @return */public synchronized static date Getnextweekday () {Calendar calendar = calendar.getinstance () ;//Calendar.set (Calendar.day_of_week, calendar.sunday+7); Calendar.add (Calendar.date, Getcurrentplus () + 1 + 6);Return Calendar.gettime () with the date of the current date plus the number of days that the current date differs from this Sunday;} /** * Get last Sunday date * * @return */public synchronized static date Getnextmonday () {Calendar calendar = calendar.getinstance (); Calendar.set (Calendar.day_of_week, calendar.sunday+7); Calendar.add (Calendar.date, Getcurrentplus () + 1);// Return Calendar.gettime () with the date of the current date plus the number of days that the current date differs from this Sunday;} /** * Gets the number of days between the current date and the week Sunday * * @return */public synchronized static int getcurrentplus () {Calendar calendar = Calendar.geti Nstance (); int days = Calendar.get (Calendar.day_of_week)-1;//in China is already Monday as the first day of the week, so here minus 1return 7-days;} public synchronized static int getcurrentyear () {Calendar calendar = calendar.getinstance (); return Calendar.get ( Calendar.year);} /** * Returns the first day of the month for the specified date * * @param date * @return */@SuppressWarnings ("static-access") public synchronized static date GetFirst Dayinmonth (date date) {Calendar calendar = calendar.getinstance (); Calendar.settime (date); Calendar.set (Calendar. DATE, 1); return Calendar.gettime ();} /** * Returns the last day of the month on the specified date * * @param datE * @return */@SuppressWarnings ("static-access") public synchronized static date Getlastdayinmonth (date date) {Calendar Calendar = Calendar.getinstance (); Calendar.settime (date); Calendar.add (Calendar. MONTH, 1); Calendar.set (Calendar. DATE, 1); Calendar.add (Calendar. DATE,-1); return Calendar.gettime ();} /* * Calculates the number of days between two dates */public static int getdaysbetween (Calendar d1, calendar D2) {if (D1.after (D2)) {Java.util.Calendar swap = D1;D1 = D2;d2 = swap;} int days = D2.get (calendar.day_of_year)-D1.get (calendar.day_of_year); int y2 = D2.get (calendar.year); if (D1.get ( calendar.year)! = y2) {D1 = (Calendar) d1.clone ();d o {days + = D1.getactualmaximum (calendar.day_of_year);// Get the actual days of the year D1.add (Calendar.year, 1);} while (D1.get (calendar.year)! = y2);} return days;} /* * Calculates the time between two dates */public static String getgaptime (date createtime) {//TODO auto-generated method Stubcalendar D2 = Calen Dar.getinstance (); Calendar D1 = calendar.getinstance ();d 1.setTime (createtime); int days = D2.get (calendar.day_of_year)-D1.get (CAlendar.  Day_of_year); if (days < 1) {int hour = D2.get (calendar.hour_of_day)-D1.get (Calendar.hour_of_day); if (hour < 1) {int Munites = D2.get (calendar.minute)-D1.get (Calendar.minute); if (munites = = 0) {return "just";} else {return munites + "minutes ago" ;}} else {return hour + "hour Ago";}} else {return days + "day before";}} Add days public static String AddDays (Int. day) {Calendar calendar = calendar.getinstance (); Calendar.add (calendar.day_of_ MONTH, day); StringBuffer sb = new StringBuffer (), Sb.append (Calendar.get (Calendar.year)). Append ("-"); Sb.append (Calendar.get ( Calendar.month) + 1). Append ("-"); Sb.append (Calendar.get (Calendar.day_of_month)); return sb.tostring ();}     Add years public static string AddYears (string now, int year) throws parseexception{Calendar fromcal=calendar.getinstance ();   DateFormat dateformat=new SimpleDateFormat ("Yyyy-mm-dd");   Date Date=dateformat.parse (now);   Fromcal.settime (date);      Fromcal.add (Calendar.year, year); Return Dateformat.format (Fromcal.gettime ()); }//plus days (specific time) publicstatic string Adddate (String now, Int. day) throws parseexception{Calendar fromcal=calendar.getinstance ();   DateFormat dateformat=new SimpleDateFormat ("Yyyy-mm-dd");   Date Date=dateformat.parse (now);   Fromcal.settime (date);      Fromcal.add (Calendar.date, day); Return Dateformat.format (Fromcal.gettime ()); }/** * How much time * @throws parseexception * * * @SuppressWarnings ("deprecation") public static void main (string[] args) throws Par seexception {System.out.println (Timerutil.addyears ("2008-12-01", 7));}   /*public static void Main (string[] args) {String createdate = "2008-12-01";   SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");    try {Date date = Sdf.parse (CreateDate);    Calendar cl = Calendar.getinstance ();    Cl.settime (date);    Cl.add (calendar.date,31);    String temp = "";    temp = Sdf.format (Cl.gettime ());   SYSTEM.OUT.PRINTLN (temp);   } catch (ParseException e) {//TODO auto-generated catch block E.printstacktrace (); }}*/}

Frequently used dates in Java format (full)

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.