Operation of the date in Java

xiaoxiao2021-03-17  206

Http://www.54he.com He Haiping hp54@163.com

Operation of the date in Java

Operation in Java .......................................... ................................................ .......... 1

1 Date Template ........................................... ................................................ ..................... 2

2 formatted date .......................................... ................................................ ................... 2

3 get the current date .......................................... ................................................ ............. 3

4 Current time ........................................... ................................................ .....................

5 1 year ago ........................................... ................................................ ..................... 4

6 Tomorrow Date ............................................. ................................................ ..................... 4

7 two times between the days ........................................ ................................................ ......... 4

8 additional half an hour ............................................ ................................................ ...................... 4

9 years of the date of summal delivery .......................................... ................................................ ................. 5

10 Seeking a few days ........................................... ................................................ ................ 5

Http://www.54he.com He Haiping hp54@163.com

11 Java data type conversion ........................................... ................................................ ......... 5

11.1 How to convert a string string into an integer int ...................................... ............................. 5

11.2 How to convert an integer int convert into a string string ...................................... ............................................................................................................................................. ................................................ ...........................................

11.4 Data Type Conversion Function ........................................... ................................................ 7

12 get the serial number plus one ........................................... ................................................ .............. 8

1 Date Template

The template is defined as follows:

Y years, such as 1996

M month, such as July or 07

Di Month Day, such as 12

H hours (24 system), such as 0,17

m minutes, such as 32

s banknote bell, such as 55

S microte banknote, such as 978

E-week, such as Tuesday

D for the year, such as 189

W Week in Year (Number) 27

W Week in Month (Number) 2

A am / pm marker (text) PM

K Hour in Day (1 ~ 24) (Number) 24

K Hour in AM / PM (0 ~ 11) (Number) 0

Z Time Zone (Text) Pacific Standard Time

'escape for text (Delimiter)

'' Single Quote (Literal) '

2 formatted date

Using the SimpleDateFormat class formatted date, if the date format is wrong, an exception is generated.

Import java.text. *;

Import java.util. *;

Public class testdate {

Public static void main (String [] args) {

String DSTR = "2001.12.12-08.23.21";

Date D = NULL;

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

Try {

Http://www.54he.com He Haiping hp54@163.com

D = SDF.PARSE (DSTR);

} catch (parseException pe) {

System.out.println (pe.getMessage ());

}

System.out.println (d);

System.out.println (d.gettime ());

}

}

Find the SimpleDateFormat class in the java.text package, give it a template, you can parse into a Date, then use DATE

The geTITME () method of the class can get a long data.

3 get the current date

Public string getdatetime ()

{

Calendar Cal = Calendar.getInstance ();

SimpleDateFormat Formatter = New SimpleDateFormat ("YYYY- MM-DD HH: MM: SS"); string mdatetime = formatter.format (Cal.getTime ());

Return (MDATETIME);

}

Public java.sql.date getdate ()

{

Java.sql.date mdate;

Calendar Cal = Calendar.getInstance ();

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

String mdatetime = formatter.format (caver.gettime ());

Return (java.sql.date.valueof (mdatetime));

}

4 current time

Calendar Cal = Calendar.getInstance ();

// simpledateformat formatter = new SimpleDateFormat ("YYYY-MM-DD HH: MM: SS");

Http://www.54he.com He Haiping hp54@163.com

SimpleDateFormat Formatter = New SimpleDateFormat ("YYYY-MM-DD HH: MM: SS G E D f W W A E f");

String mdatetime = formatter.format (caver.gettime ());

Out.println (MDATETIME);

Out.println ("
");

5 1 year ago

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

Long myTime = (mydate.gettime () / 1000) -60 * 60 * 24 * 365;

mydate.settime (MyTime * 1000);

String mdate = formatter.format (mydate);

Out.println (MDATE);

Out.println ("
");

6 tomorrow

mydate = new java.util.date ();

MyTime = (mydate.gettime () / 1000) 60 * 60 * 24;

mydate.settime (MyTime * 1000);

MDATE = formatter.format (mydate);

Out.println (MDATE);

Out.println ("
");

7 days between two times

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

Java.util.date date = myformatter.parse ("2003-05-1");

Java.util.date mydate = myFormatter.Parse ("1899-12-30");

Long day = (Date.getTime () - mydate.gettime ()) / (24 * 60 * 60 * 1000);

Out.println (DAY);

Out.println ("
");

8 additional half an hour

SimpleDateFormat Format = New SimpleDateFormat ("YYYY-MM-DD HH: MM: SS"); java.util.date date1 = format.parse ("2002-02-28 23:16:00);

Long Time = (Date1.gettime () / 1000) 60 * 30;

Date1.settime (Time * 1000);

Http://www.54he.com He Haiping hp54@163.com

String mydate1 = formatter.format (date1);

Out.println (MyDate1);

Out.println ("
");

9 months

SimpleDateFormat Formatter2 = New SimpleDateFormat ("YYYY-MM F E");

Java.util.date Date2 = formatter2.parse ("2003-05 5 Friday");

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

String mydate2 = formatter3.Format (date2);

Out.println (mydate2);

Out.println ("
");

10 Seeking the week

mydate = myformatter.parse ("2001-1-1");

SimpleDateFormat Formatter4 = New SimpleDateFormat ("E");

String mydate3 = formatter4.format (mydate);

Out.println (MyDate3);

Out.println ("
");

11 Java data type conversion

11.1 How to convert a string string into an integer INT

1). INT i = integer.parseint ([string]); or

I = integer.parseint ([String], [INT RADIX]);

2). INT i = integer.valueof (my_str) .intValue ();

Note: The string is transformed into Double, Float, and long.

11.2 How to convert an integer Int into a string string String

A. Have a variety of methods:

String s = String.Valueof (i);

2.) String s = integer.tostring (i);

Http://www.54he.com He Haiping hp54@163.com

3.) String s = "" i;

Note: Double, Float, long-converted to string is similar.

11.3 examples

Package cn.com.lwkj.rts.Register;

Import java.sql.date;

Public class typechange {

Public typechange () {

}

// Change the string type to the int TYPE

Public Static int StringToint (String IntStr)

{

Integer integer;

Integer = integer.valueof (INTST);

Return Integer.intValue ();

}

// Change int Type to the string typepublic static string INTSTOSTRING (INT VALUE)

{

Integer Integer = New Integer (Value);

Return integer.toString ();

}

// Change the string type to the float type

Public static float stringtofloat (String floatstr)

{

Float floatee;

FLOATEE = float.valueof (floatstr);

Return floatee.floatvalue ();

}

// Change the float type to the string type

Public Static String floattostring (Float Value)

Http://www.54he.com He Haiping hp54@163.com

{

Float floatee = new float (value);

Return floatee.tostring ();

}

// Change the string type to the sqldate type

Public static java.sql.date stringtodate (String DateStr)

{

Return java.sql.date.valueof (dateStr);

}

// change the sqldate type to the string type

Public Static String DateTString (java.sql.date datee)

{

Return Datee.toString ();

}

Public static void main (string [] args)

{

Java.sql.date day;

Day = TypeChange.StringTodate ("2003-11-3");

String strday = typechange.datetostring (day);

System.out.println (strDay);

}

}

11.4 Data Type Conversion Function

Data type conversion function in Java

Although it can be found in the Java API, make a backup.

String-> Byte

Byte Static Byte ParsebyTe (String S)

BYTE-> STRING

Byte Static String Tostring (byte b)

Char-> string

Http://www.54he.com He Haiping hp54@163.com

Character Static String to String (Char C)

String-> short

Short Static Short Parseshort (String S)

Short-> string

Short Static String Tostring (SHORT S)

String-> Integer

Integer Static Int PaSeint (String S)

Integer-> String

Integer Static String Tostring (INT i)

String-> long

Long Static Long Parselong (String S)

Long-> String

Long Static String Tostring (long i)

String-> float

Float Static Float Parsefloat (String S)

FLOAT-> String

Float static string toString (float f) String-> DOUBLE

Double Static Double Parsedouble (String S)

Double-> String

Double Static String Tostring (double)

12 get the serial number plus one

Public int getMaxid (String Vtablename, String VfieldName)

{

INT mResult = 0;

Boolean mconn = true;

String msql = new string ();

MSQL = "SELECT MAX (" vfieldname ") 1 as maxid from" vtablename

Try

{

Http://www.54he.com He Haiping hp54@163.com

IF (conn! = null) {

Mconn = conn.isclosed ();

}

IF (mconn) {

OpenConnection ();

}

ResultSet Result = ExecuteQuery (MSQL);

IF (result.next ())

{

MRESULT = Result.Getint ("MaxID");

}

Result.close ();

IF (MCONN)

{

CloseConnection ();

}

}

Catch (Exception E)

{

System.out.println (E.TOString ());

}

Return (MRESULT);

}

MDocumentId = dbaobj.getmaxid ("Document", "DocumentID");

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

New Post(0)