A variety of operations on time (reproduced)

xiaoxiao2021-03-06  39

Package Web.com;

Import java.util.calendar;

Import java.util.date;

Import java.text. *;

Public class datetime {

Public datetime () {

}

/ **

* Take the current time of the system

* @Return String YYYY-MM-DD

* /

Public String getcurrentdate () {

Calendar rightnow = calendar.getinstance ();

INT year = rightnow.get (rightnow.year);

INT MONTH = RightNow.get (rightnow.mont) 1;

Int day = rightnow.get (rightnow.date);

Return Year "-" MONTH "-" DAY;

}

/ **

* Take the current time of the system

* @Return String YYYY MM Moon DD Day

* /

Public string getcurrentdate1 () {

Calendar rightnow = calendar.getinstance ();

INT year = rightnow.get (rightnow.year);

INT MONTH = RightNow.get (rightnow.mont) 1;

Int day = rightnow.get (rightnow.date);

Return Year "Year" Month "Month" Day "Day";

}

/ **

* Take the current time of the system

* @Return string YYYYMMDD

* /

Public string getcurrentdate22 () {

Calendar rightnow = calendar.getinstance ();

INT year = rightnow.get (rightnow.year);

INT MONTH = RightNow.get (rightnow.mont) 1;

Int day = rightnow.get (rightnow.date);

Return Year " Month " DAY;

}

/ **

* Take the current time of the system

* @Return String YYYY-MM

* /

Public string getcurrentdate3 () {

Calendar rightnow = calendar.getinstance ();

INT year = rightnow.get (rightnow.year);

INT MONTH = RightNow.get (rightnow.mont) 1;

// int Day = rightnow.get (rightnow.date);

RETURN YEAR "-" MONTH;

}

/ **

* Take the current time of the system

* @Return String YYYYMMDDHMMSS

* /

Public string getcurrentdate4 () {

SimpleDateFormat SimpleDateFormat = New SimpleDateFormat ("YYYYMMDDHMMMS");

Java.util.date Date = new java.util.date (); string time = simpledateformat.format (date);

Return Time;

}

/ **

* Take the current time of the system

* @Return String YYYY-MM-DD HH: MM: SS

* /

Public string getcurrentdate5 () {

SimpleDateFormat SimpleDateFormat = New SimpleDateFormat ("YYYY-MM-DD HH: MM: SS");

Java.util.date Date = new java.util.date ();

String Time = SimpleDateFormat.Format (date);

Return Time;

}

/ **

* A one-day day before the current time of the system

* @Param n int

* @Return String YYYY-MM-DD

* /

Public string getnmonthbeforecurrentday (int N) {

Calendar c = calendar.getInstance ();

C.ADD (C.MONTH, -N);

Return "" C. Get (C.Year) "-" (C.Get (C.MONTH) 1) "-" C.Get (C.DATE);

}

/ **

* A day after the current time of the system is obtained.

* @Param n int

* @Return String YYYY-MM-DD

* /

Public String getnmonthaftercurrentday (int N) {

Calendar c = calendar.getInstance ();

C.ADD (C.MONTH, N);

Return "" C. Get (C.Year) "-" (C.Get (C.MONTH) 1) "-" C.Get (C.DATE);

}

/ **

* N days before the system is current time

* @Param n int

* @Return String YYYY-MM-DD

* /

Public string getndaybeforecurrentdate (int N) {

Calendar c = calendar.getInstance ();

C.ADD (c.day_of_month, -n);

Return "" C. Get (C.Year) "-" (C.Get (C.MONTH) 1) "-" C.Get (C.DATE);

}

/ **

* N days after the system is current time

* @Param n int

* @Return String YYYY-MM-DD

* /

Public String getndayAftercurrentDate (int N) {

Calendar c = calendar.getInstance ();

C.ADD (C.DAY_OF_MONTH, N);

Return "" C. Get (C.Year) "-" (C.Get (C.MONTH) 1) "-" C.Get (C.DATE);

}

/ / -------------------------------------------------------------------------------------------- --------------------- // Take a day of the system that the system is corresponding to the day.

Public string getcurrentdateAfterpastdate (String spastdate) {

IF (SpastDate! = Null &&! Spastdate.equals (")) {

Date Date = SwitchStringTodate (Spastdate);

Calendar c = calendar.getInstance ();

C.SetTime (date);

INT ipastyear = c.get (c.year);

Calendar C1 = Calendar.getInstance ();

Int icrrentyear = c1.get (c1.year);

C.ADD (C.Year, Icurrentyear - iPastyEar);

Return "" C.Get (C.Year) "-" (C.Get (C.MONTH) 1) "-"

C. Get (C.DATE);

}

Else {

Return NULL;

}

}

/ **

* Transform a date string into date

* @Param SDATE STRING

* @Return Date YYYY-MM-DD

* /

Public Date SwitchStringTodate (String SDATE) {

Date Date = NULL;

Try {

SimpleDateFormat DF = New SimpleDateFormat ("YYYY-MM-DD");

Date = DF.PARSE (SDATE);

}

Catch (Exception E) {

System.out.println ("Date Transition Failed:" E.GetMessage ());

}

Return Date;

}

/ **

* Enter the date of two string types, compare the size of both

* @Param fromDate String

* @Param Todate String

* @Return Boolean Before True

* /

Public Boolean ComparetWodateBigorsmall (String fromDate, String Todate) {

Date DateFrom = this.switchStringTodate (fromDate);

Date DateTo = this.switchStringTodate (TODATE);

IF (DateFrom.Before (Dateto)) {

Return True;

}

Else {

Return False;

}

}

/ **

* Transform a date string into Calendar

* @Param SDATE STRING

* @Return Calendar

* /

Public Calendar SwitchStringTocalendar (String SDATE) {

Date Date = SwitchStringTodate (SDATE);

Calendar c = calendar.getInstance ();

C.SetTime (date);

Return C;

}

/ **

* Translate a date into Calendar

* @Param Date Date

* @Return Calendar

* /

Public Calendar SwitchStringTocalendar (Date Date) {

Calendar c = calendar.getInstance ();

C.SetTime (date);

Return C;

}

Public string string2date (String DateString)

Throws java.lang.exception {

/ * DateFormat DateFormat;

DateFormat = New SimpleDateFormat ("YYYY-MM-DD KK: MM: SS");

DateFormat.Setlenient (false);

Java.util.date TIMEDATE = DATEFORMAT.PARSE (DATESTRING); // Util Type

//java.sql.date datetime = new java.sql.date (TIMEDATE.GETTIME ()); // SQL type

Java.sql.timestamp datetime = new java.sql.timestamp (TIMEDATE.GETTIME ()); // TimeStamp type

* /

SimpleDateFormat Newk = new SimpleDateFormat ("YYYY.MM.DD HH: MM: SS");

SimpleDateFormat Old = New SimpleDateFormat ("MMM DD HH: MM: SS YYYY");

String strdate = "Mar 22 00:42:00 2002";

Date D = Old.Parse (strdate);

System.out.println (Newk.Format (D));

Return newk.format (d);

}

/ **

* Before getting a certain day before N month

* @Param SDATE STRING

* @Param n int

* @Return String YYYY-MM-DD

* /

Public String getnmonthbeforeOnEDay (string sdate, int N) {

Calendar C = SwitchStringTocalendar (SDATE);

C.ADD (C.MONTH, -N);

Return "" C. Get (C.Year) "-" (C.Get (C.MONTH) 1) "-" C.Get (C.DATE);

}

/ / Get a certain day after a certain time N month

Public string getnmonthafteroneday (string sdate, int N) {

Calendar C = SwitchStringTocalendar (SDATE);

C.ADD (C.MONTH, N);

Return "" C. Get (C.Year) "-" (C.Get (C.MONTH) 1) "-" C.Get (C.DATE);

}

/ / Get a certain time n days, format YYYY-MM-DD

Public String getndaybeforeOnEDate (string sdate, int n) {

Calendar C = SwitchStringTocalendar (SDATE);

C.ADD (c.day_of_month, -n);

Return "" C.Get (C.Year) "-" (C.Get (C.MONTH) 1) "-" C.Get (C.DATE);

// N days after getting a certain time, format YYYY-MM-DD

Public string getndayAfterONEDATE (String sdate, int N) {

Calendar C = SwitchStringTocalendar (SDATE);

C.ADD (C.DAY_OF_MONTH, N);

Return "" C. Get (C.Year) "-" (C.Get (C.MONTH) 1) "-" C.Get (C.DATE);

}

// Judgment the current time is not annihilation

Public boolean isrunnian () {

Java.util.calendar RightNow = java.util.calendar.GetInstance ();

INT year = rightnow.get (rightnow.year);

IF (0 == Year% 4 && (Year% 100! = 0 || Year% 400 == 0)) {

Return True;

}

Else {

Return False;

}

}

Public static void main (string args []) {

DateTime a = new datetime ();

Try {

//System.out.print (a.string2date(a.string2date)));

} catch (exception e) {system.err.println ();

}

}

转载请注明原文地址:https://www.9cbs.com/read-66660.html

New Post(0)