Learn Java date, calendar date operations _java

Source: Internet
Author: User
Tags locale

This article describes the date-related operations in the Java development process, sharing the following 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 Operation * * @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 Formatted * @param date * @return/public static String datetostring (date date) {//simpled
Ateformat format = new SimpleDateFormat ("Y year mm month DD Day E hh" mm minute ss seconds ", Locale.china);
    SimpleDateFormat format = new SimpleDateFormat ("Y-year m-month D-day E h when M minute s sec", Locale.china); SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss", Locale.china);
  Example return Sdf.format (date);
   /** * Date Formatted * @param date * @return*/public static String datetostring (Calendar cal) {return datetostring (Cal.gettime ()); /** * DateString Calendar * * @param date * format:2015-06-16 date * @return Calendar * @t Hrows parseexception */public static Calendar Datestringtocalendar (String datestr) throws ParseException {//Ca
Lendar 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");
    Date date = Sdf.parse (DATESTR);
    Calendar cal = Calendar.getinstance ();
    Cal.settime (date);
  Return cal;
   /** * datestring 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 CA
  L) {return cal.gettime (); /** * Calculates the difference between two dates * * @param date * C1 * @param date * C2 * @return Int[]{year, Mont
    H, 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); /** * Calculates the difference between two dates * * @param calendar * C1 * @param calendar * C2 * @return Int[]{ye AR, Month, day} */public int[] CalculatedifferDay (Calendar c1, calendar C2) {int[] P1 = {C1.get (calendar.year), C1.get (Calendar.month), C1.get (calendar.day_of_m
    Onth)};
    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] * () + p2[1]-((p1[0] *) + p1[1]);
    int day = (int) ((C2.gettimeinmillis ()-C1.gettimeinmillis ())/(24 * 60 * 60 * 1000));
  Return to new int[] {year, month, day};
  /** * Get date on the first day of the week * * @param c * @return/public static calendar Getlastdayofweek (Calendar C)  {//SimpleDateFormat FORMAT2 = new SimpleDateFormat ("Y year M month D Day E h time m minute s sec", 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 * @return/public static int getlastdayofmonth (int yea
    R, 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) {RE
  Turn cal.getactualmaximum (calendar.day_of_month);

 }
   
}

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.