Problems with data types that are often encountered in the Java development process

Source: Internet
Author: User
Tags date dateformat integer sql
Process | data | data type | problem (1) when converting an int or double to BigDecimal, the number of digits increases accordingly, in order to solve this problem, you can round the double long type by round the

After
, convert to string and then convert it via New BigDecimal ()


   


For example: Fosum = new BigDecimal (string.& #118alueOf (Round (uo1sum.double& #118alue () + uo2sum.double& #118alue (), 3))


 


(2) converts time to character

Java.util.Date Date = new Java.util.Date (DATABEAN.GETTYRQ (). GetTime ());
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
TYRQ = Sdf.format (date);

(3) Converting a string into a time timestamp type
Public Java.sql.Timestamp Strtotimestamp (String str) {
Java.sql.Timestamp sdate = null;

if (str!=null) {

if (Str.trim (). Length () ==8) {
str = str.substring (0,4) + "-" +str.substring (4,6) + "-" +str.substring (6,8);

}
try{
DateFormat df = dateformat.getdateinstance (Dateformat.default,java.util.locale.china);
Java.util.Date Date = Df.parse (str);
Sdate = new Java.sql.Timestamp (Date.gettime ());
}catch (Exception e) {
E.printstacktrace ();
}

}
return sdate;
}

(4) rounding the double type
Public double round (double V,int scale) {

if (scale<0) {

throw new IllegalArgumentException ("The scale must be a positive integer or zero");

}

BigDecimal B = New BigDecimal (double.tostring (v));

BigDecimal one = new BigDecimal ("1");

Return B.divide (one,scale,bigdecimal.round_half_up). double& #118alue ();

}
(5) Convert int to byte[]

Public byte[] Inttobytearray (int num) {
int temp = num;
Byte[] B = new Byte[6];
for (int i=b.length;i>-1;i--) {
B[i] = new Integer (temp&0xff). byte& #118alue ();
temp = temp >> 8;
}
return b;
}

(6) Convert int to byte
int s=0;
byte B = (byte) s;







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.