Java date operation, date, Calendar operation

Source: Internet
Author: User
Tags locale


Java development can not avoid the date-related operations, here summarizes some common methods ~

Directly on the code:

Package jse;import java.io.unsupportedencodingexception;import java.text.parseexception;import  java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import  java.util.locale;/** *  Common date Operations  *  *  @author  puck * createdate  2015-07-30 22:54:38 */public class testdate{public static void main ( String[] args)  throws ParseException, UnsupportedEncodingException{Calendar cal  = calendar.getinstance ();//cal.add (calendar.day_of_month, -48); System.out.println (Datetostring (CAL));} /** *  date formatting  *  @param  date *  @return  */public static string  datetostring (date date) {//simpledateformat format = new simpledateformat (" y mm month dd Day  e hh mm min ss sec ",  locale.china);//simpledateformat format = new  SimpleDateFormat ("Y year M month D-Day  e&nbsP H M min s second ",  locale.china); Simpledateformat sdf = new simpledateformat ("Yyyy-mm-dd hh:mm:ss",  Locale.CHINA) ;  // examplereturn sdf.format (date);} /** *  date formatting  *  @param  date *  @return  */public static string  datetostring (calendar cal) {return datetostring (Cal.gettime ());} /** * datestring  Transfer  Calendar *  *  @param  Date *             format:2015-06-16 date *  @return  Calendar *  @throws  ParseException */public static Calendar  Datestringtocalendar (STRING DATESTR)  throws ParseException{//Calendar cal =  Calendar.getinstance ();//cal.clear ();//cal.set (Integer.parseint (date.substring)) 0, 4 Integer.parseint (date.substring (5, 7))  - 1,//integer.parseint (date.substring (8, 10)));//return cal; Simpledateformat sdf = new simpledateformat ("Yyyy-mm-dd hh:mm:ss");D ate date  = sdf.parse (DATESTR); Calendar cal = calendar.getinstance (); Cal.settime (date); return cal;} /** * datestring  Transfer  Date *  *  @param  Date *             format:yyyy-mm-dd hh:mm:ss date * @ return calendar *  @throws  ParseException */public static Date  Datestringtodate (string date)  throws parseexception{simpledateformat sdf = new  simpledateformat ("Yyyy-mm-dd hh:mm:ss"); Return sdf.parse (date);} /** * date convert to calendar *  *  @param  date *  @return  */public static calendar datetocalendar (date date) {calendar c1 =  calendar.getinstance (); C1.settime (date); return c1;} /** * calendar convert to date *  @param  cal *  @return  * /public static date calendartodate (calendar cal) {return cal.gettime ();} /** *  Calculation Two date difference month date  *  *  @param  Date *             c1 *  @param  Date *             c2 *  @return  int[]{year, month, day} * /public int[] calculatedifferday (DATE D1, DATE D2) {calendar c1 =  Calendar.getinstance (); C1.settime (D1); Calendar c2 = calendar.getinstance (); C1.settime (D2); Return calculatedifferday (C1, C2) ;} /** *  Calculation Two date difference month date  *  *  @param  Calendar *             c1 *  @param   calendar *            c2 *  @return  int[]{year, month, day} */public int[] calculatedifferday (CALENDAR C1,  CALENDAR C2) {int[] p1 = { c1.get (calendar.year),  c1.get (Calendar.MONTH),  c1.get (Calendar.day_of_month)  };int[] p2 = { c2.get (Calendar.YEAR),  c2.get (Calendar.month),  c2.get (Calendar.day_of_month)  }; System.out.println ("p1[0]="  + p1[0] +  " p1[1]="  + p1[1] +  "  P1[2]= " + p1[2]); System.out.println ("p2[0]="  + p2[0] +  " p2[1]="  + p2[1] +  "  P2[2]= " + p2[2]);int year = p2[0] - p1[0];int month =  (p2[0]  * 12)  + p2[1] -  ((p1[0] * 12)  + p1[1]); int day =   (int)   ((C2.gettimeinmillis () &Nbsp;- c1.gettimeinmillis ())  /  (24 * 60 * 60 * 1000)); return  new int[] { year, month, day };} /** *  Get date first day of week  *  *  @param  c *  @return  */public  Static calendar getlastdayofweek (calendar c) {//simpledateformat format2 = new  simpledateformat ("Y year M month D Day  e h m minute s seconds",  locale.china);//system.out.println ("Current Time:"  +  format2.format (C.gettime ())); C.set (Calendar.day_of_week, calendar.monday);//System.out.println (" Monday Time: " + format2.format (C.gettime ())); return c;} /** *  date plus minus  *  @param  c *  @param  day *  @return  */public  static calendar addordecreaseday (calendar c, int day) {C.add (Calendar.DAY_OF_ Month, day); return c;} /** *  get the last day of the month  *  @param  year *  @param  month *&nBSP; @return  */public static int getlastdayofmonth (Int year, int month) { Calendar c = calendar.getinstance (); C.set (year, month - 1, 1);return  C.getactualmaximum (calendar.day_of_month);} /** *  get the last day of the month  *  @param  cal *  @return  */public static int  getlastdayofmonth (calendar cal) {return cal.getactualmaximum (calendar.day_of_month);}}

Welcome to correct, add Kazakhstan


Java date operation, date, Calendar operation

Related Article

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.