java data type conversion

2023-01-05   ES  

  1. int i = Integer.parseInt(String str);

int i = Integer.valueOf(String str).intValue();

Note: Integer.parseint is different from Integer.Valueof. The former generates integer, while the latter is an object, so the value of the object must be obtained by intvalue ();

The method of

string to double, float, long is similar.
2. Surgery converted into string:

String str = String.valueOf(int i);

String str = Integer.toString(int i);

String str = “” + i ;

Note: Double, Float, Long’s method is similar.
3. Long conversion to date:

SimpleDateFormat sf = new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss”);

// tieml, timef is long, which is transformed before

Date date = new Date(timel – timef);

String time = sf.parse(date);

1, Float type converted to Double type:

float f1=100.00f;

Float F1=new Float(f1);

//f1.doubleValue () is a way to return Double value types of the Float class

double d1=F1.doubleValue();

2, dough type converted to int type:

double d1=100.00;

Double D1=new Double(d1);

int i1=D1.intValue();

3, INT type converted to Double type:

int i1=200;

double d1=i1;

(2). The conversion of string and other data types

4. Conversion between string and other types:

⑴ conversion of other types to string

① Class conversion method of calling: x.tostring ();

② Automatic conversion: x+””;

③ Use String’s method: string.valueof (x);

⑵ String as a value, conversion to other types

① first convert to the corresponding packaging instance, and then call the corresponding method to convert to other types

For example, the format of the value of “32.1” in characters in characters is: New Float (“32.1”). DoubleValue ().

can also be used: double.valueof (“32.1”). DoubleValue ()

② Static PARSEXXX method

String s = “1”;

byte b = Byte.parseByte( s );

short t = Short.parseShort( s );

int i = Integer.parseInt( s );

long l = Long.parseLong( s );

Float f = Float.parseFloat( s );

Double d = Double.parseDouble( s );

③ GetNumericValue (Char CH) method of connumericValue (Char CH)

Return to the INT value represented by the specified unicode character.

(3). Other practical data type conversion

5. Date class and other data types mutual conversion

There is no direct correspondence between the

integer and the date, but you can use the INT type to represent the year, month, day, time, time, minute, second, so as to establish a corresponding relationship between the two In this conversion, you can use the three forms of the Date class constructor:

①Date (int Year, int Month, int Date): expressed in the int type, month, month, and day

②Date (int Year, int Month, int Date, int Hrs, int Min):

③Date (int Year, int Month, int Date, int HRS, int Min, int SEC):

, second

There is a very interesting correspondence between

between the long integer and the Date class, which is to indicate a time as the milliseconds at 0:0 % on January 1, 1970 at the standard time. For this corresponding relationship, the Date class also has its corresponding constructor: date (long date).

Get the year, month, day, time, time, minute, second, and

Week you can use the DATE class Getyear (), Getmonth (), GetDate (), GetHours (), Getminutes (), GetSeconds (), GetDay () method, you can also understand it as a date class to int.

and the Date class Gettime () method can get a long integer corresponding to the time we mentioned earlier. Like the packaging class, the Date class also has a Tostring () method to convert it to the String class.

Sometimes we want to get the specific format of Date, such as 20020324, we can use the following methods, first introduce in the file,

import java.text.SimpleDateFormat;

import java.util.*;

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

// If you want to get yyyymmdd format

SimpleDateFormat sdf=new SimpleDateFormat(“yyyyMMDD”);

String dateFormat=sdf.format(date);

// If you want to be separated from the year, month, day

SimpleDateFormat sy=newSimpleDateFormat(“yyyy”);

SimpleDateFormat sm=new SimpleDateFormat(“MM”);

SimpleDateFormat sd=new SimpleDateFormat(“dd”);

String syear=sy.format(date);

String smon=sm.format(date);

String sday=sd.format(date);

// JS convert DATA into String type

  • Expansion of Date, transform the date into string in the specified format

  • month (m), day (d), 12 hours (h), 24 hours (h), (m), second (s), week (e), quarter (q) can be used 1-2 Bital

  • year (Y) can use 1-4 placeholders, milliseconds (s) can only be used with 1-3 digits (1-3 digits)

*eg:

  • (new Date()).pattern(“yyyy-MM-dd hh:mm:ss.S”) ==>2006-07-0208:09:04.423

  • (new date ()). Pattern (“Yyyy-MM-DD E HH: SS”) ==> 2009-03-10 two 20:09:04

  • (new date ()). Pattern (“Yyyy-MM-DD EE HH: SS”) ==> 2009-03-10 Tuesday 08:09:04

  • (new date ()). Pattern (“Yyyy-MM-DD EEE HH: SS”) ==> 2009-03-10 Tuesday 08:09:04

  • (new Date()).pattern(“yyyy-M-d h️s.S”) ==>2006-7-2 8:9:4.

6. Character-type turning time type: (2005-10-1)

reportdate_str = “2005-10-01”;

reportdate_str = reportdate_str + “ 00:00:00.0”

reportdate = java.sql.Timestamp.valueOf(reportdate_str);

7. Timetable transit character type:

V_DATE = reportdate.toString();

8. Convert LONG type to String

long APP_UNIT = (long)userview.getDEPT_ID();

String S_APP_UNIT = String.valeOf(APP_UNIT);

Basic type S can be converted into string through string.valueof (s).

string->byte

Byte static byte parseByte(String s) byte->string

Byte static String toString(byte b)char->string

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 parseInt(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)

  1. int length data types are: Byte (8bits), Short (16bits), int (32bits), long (64bits),

  2. Float length data types are: single accuracy (32bits float), dual -precision (64bits double)

  3. BOOLEAN type variables are: Ture, FALSE

  4. CHAR data types are: Unicode character, 16 -bit

  5. corresponding types: Integer, Float, Boolean, Character, Double, Short, Byte, Long

From low -precision to high -precision conversion

byte 、short、int、long、float、double、char

Note: When the two CHAR type operations, it is automatically converted to int type; when Char and other types of operations are also automatically converted to int

Positive conversion: Come out a new type of variable through the packaging device

Integer a= new Integer(2);

reverse conversion: transform through a type of packaging

int b=a.intValue();

Positive conversion: Because each class is a subclass of the Object class, and all Object classes have a Tostring () function, so you can convert it through the Tostring () function

Inverse conversion: A new type of variable is produced through the type of packaging.

eg1: int i=Integer.valueOf(“123”).intValue()

Instructions: The above example is to convert a string into an Integer object, and then call the INT value of the INT value of this object.

eg2: float f=Float.valueOf(“123”).floatValue()

Instructions: The above example is to convert a string into a float object, and then call the Floatvalue () method of this object to return its corresponding float value.

eg3: boolean b=Boolean.valueOf(“123”).booleanValue()

Instructions: The above example is to convert a string into a Boolean object, and then call the Booleanvalue () method of this object to return its corresponding Boolean value.

eg4:double d=Double.valueOf(“123”).doubleValue()

Instructions: The above example is to convert a string into a double object, and then call the DoubleValue () method of this object to return its corresponding double value.

eg5: long l=Long.valueOf(“123”).longValue()

Instructions: The above example is to convert a string into a long object, and then call the longvalue () method of this object to return its corresponding Long value.

eg6: char=Character.valueOf(“123”).charValue()

Instructions: The above example is to convert a string into a Character object, and then call the Charvalue () method of this object to return its corresponding Char value.

, such as: int a = 12;

String b;b=a+””;

Through a type of packaging

eg1:int i=Integer.parseInt(“123”)

Instructions: This method can only be applied to the transformation of the string into a integer variable

eg2: float f=Float.valueOf(“123”).floatValue()

Instructions: The above example is to convert a string into a float object, and then call the Floatvalue () method of this object to return its corresponding float value.

eg3: boolean b=Boolean.valueOf(“123”).booleanValue()

Instructions: The above example is to convert a string into a Boolean object, and then call the BOOLEANVALUE () method of this object to return the corresponding

Boolean value.

eg4:double d=Double.valueOf(“123”).doubleValue()

Instructions: The above example is to convert a string into a double object, and then call the DoubleValue () method of this object to return its corresponding double value.

eg5: long l=Long.valueOf(“123”).longValue()

Instructions: The above example is to convert a string into a long object, and then call the longvalue () method of this object to return its corresponding Long value.

eg6: char=Character.valueOf(“123”).charValue()

Instructions: The above example is to convert a string into a Character object, and then call the Charvalue () method of this object to return its corresponding Char value.

source

Random Posts

splayTree’s implementation of the stretching tree

Adaptive QP (adaptive qp)

Anaconda’s Python version downgraded

web development tool class (3): jsonutils

Kotlin – Open, Final, Abstract modifier sinocrazy