Java Basics wrapper class, Math, date format processing

Source: Internet
Author: User
Tags dateformat

Wrapper class, Math, date format processing
    • Packing class
    • Math:random (), round ()
    • Date format processing

Java advocates that thought is everything, but our basic data type is not a class, that is, there is no object concept, and Java advocated the idea of violating, in order to solve this contradiction, Java provides 8 kinds of basic data types of packaging classes.

Serial number

Basic data types

Packing class

1

Int

Integer

2

Char

Character

3

Float

Float

4

Long

Long

5

Short

Short

6

Double

Double

7

Boolean

Boolean

8

Byte

Byte

     Public Static voidMain (string[] args) {intI=Ten; Boolean flag=false; Doublej=10.00; Integer k=NewInteger (i);//basic data types are converted to wrapper class objects (boxing)Boolean f=NewBoolean (flag); Double D=NewDouble (j); intX=k.intvalue ();//wrapper classes are converted to basic data types called unboxingBoolean bo=F.booleanvalue (); DoubleDe=d.doublevalue ();//to disassemble the box}

In addition to solving the ideas that Java advocates, what can you do with the packaging class?

    • • Boxing: Converting basic data types to wrapper class objects
    • • Unboxing: Converting wrapper class objects to basic data types
    • • Convert strings to basic data types

The above packing and unpacking operation is done manually, after JDK1.5, the packing and unpacking operation can be carried out automatically.

 Public Static voidMain (string[] args) {intI=Ten; Boolean flag=false; Doublej=10.00; Integer k=i;//basic data types are converted to wrapper class objects (boxing)Boolean f=Flag; Double D=J; intX=k;//wrapper classes are converted to basic data types called unboxingBoolean bo=F; DoubleDe=d;//to disassemble the box}
// Convert a string to a base data type    Private void Mian () {        //  TODO auto-generated method stub        String s="1122" ;         int x=integer.parseint (s);         int y=integer.valueof (s);    }

When developing, it is important to note that when converting, pay attention to the appearance of anomalies, for example, if we convert the string "aabb" to int type, then it will be an error (numberformatexception).

2Math

The Math class defines a series of mathematical operations, and is a method that uses the static modifier, which means that it can be accessed directly through the math class. We generally only need to focus on two of the development:

    • Rounded
    • • Get random numbers
     Public Static void Main (string[] args) {        double d=100.53;         int i = (int) Math.Round (d);        System.  out . println (i);    }

The other is the random number: Using random () to take an arbitrary number is 0-1 decimal, is greater than or equal to 0, less than 1.

Example: Take a random number between 1-10

     Public Static void Main (string[] args) {        for (int1; i++) {               Double d=math.random (); // take a random number            from 0 to 1 int x= (int) math.round (d*);            System. out. println (x+"  ");        }    }
Processing of 3rd-year formats

We are mainly talking about the date class, the date class in the Java.util package, the Calendar class, and then the date format conversion class (DateFormat).

Date:Most of the methods in the date class are obsolete, and generally only the construction method is used to get the current time of the system.

· Public Long GetTime (): Gets the number of milliseconds since the beginning of 1970, January, 1th

 Public Static void Main (string[] args) {        new  Date ();        SYSTEM.OUT.PRINTLN (date);        System.out.println (Date.gettime ());    }

What if we're going to get the exact date object's date, seconds, minutes, milliseconds? We know that the methods in the date class are obsolete, so we are now using the Calendar Calendar class to complete the operation. This class has a number of ways to do this:

· Public abstract void Add (int field, int amount): Adds the specified value to the specified calendar field

· public int get (int field): Gets the value of the specified calendar field

· Public Final Date getTime (): Convert to date type

· Public void Set (int field, int value): Specifies the value of the Calendar field setting specified

· Public final void settime (date date): sets the date type to the calendar object

Example: Accurate month-on-day seconds

 Public Static voidMain (string[] args) {Date date=NewDate ();        SYSTEM.OUT.PRINTLN (date);        System.out.println (Date.gettime ()); Calendar CA=NewGregorianCalendar ();//Get the Calendar object (you can also get the current time of the system)System.out.println ("Year" +Ca.get (calendar.year)); System.out.println ("Month" +ca.get (calendar.month) +1);//0-11 monthsSystem.out.println ("Day" +Ca.get (calendar.day_of_month)); System.out.println ("When" +Ca.get (calendar.hour_of_day)); System.out.println ("Min" +Ca.get (Calendar.minute)); System.out.println ("Seconds" +Ca.get (Calendar.second)); System.out.println ("Milliseconds" +Ca.get (Calendar.millisecond)); System.out.println ("The first few weeks of the year" +Ca.get (calendar.week_of_year)); System.out.println ("The first few weeks of the month:" +Ca.get (calendar.week_of_month)); }
3. Conversion of date formats

In Java using dateformat for date format conversion, this class is also an abstract class, when instantiated must rely on subclasses:SimpleDateFormat, commonly used construction methods:

· Public SimpleDateFormat (String pattern): Conversion rules for dates represented by parameters

Rules:y year,M-month,D-Day,H-hour,m-minute,S-second,S-millisecond

Common methods of DateFormat:

· Public final string format (date date): Converts a Date object to a string

· Public Date Parse (string source) throws ParseException: Converts a string to a date

Example: Converting a date type to a string

 Public Static void Main (string[] args) {        new  Date ();         New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss. SSS "); // Defining conversion Formats                  = Df.format (date); // to convert                 System.out.println (s);            }}

Example: Converting a string into a date

String s = "1998/10-10";         New SimpleDateFormat ("Yyyy/mm-dd"); // Defining conversion Formats        Try {            = Df.parse (s);  converts a string into a date             System.out.println (date)        ; Catch (Exception e) {            e.printstacktrace ();        }

In the development process, it is important to pay attention to the SimpleDateFormat definition of the conversion rules, if the conversion rules defined errors, then the date conversion exception can not be avoided

Summary

• Use wrapper classes to convert strings to basic data types

• Use the random () and round () methods in math

• Learn to use some of the methods in the Calendar class

• Learn to use DateFormat for conversion of date formats

Java Basics wrapper class, Math, date format processing

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.