Basic type packing class - date class

Keywords: Java

summary

Functions of basic type packaging class:

The benefit of encapsulating basic data types into objects is to define more functional methods in objects to manipulate the data

One of the common operations: used for conversion between basic data type and string

Basic data typePackaging
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

Integer class

summary

Wrap the value of the original type int in an object

Construction method

Method namedescribe
public static Integer valueOf(int i)Returns an Integer instance representing the specified int value
public static Integer valueOf(String s)Returns an Integer object String that holds the specified value

example:

public class Demo {
    public static void main(String[] args) {
        Integer s= Integer.valueOf(30);
        System.out.println(s);
        Integer s1=Integer.valueOf("20");
        System.out.println(s1);
        }
    }

Conversion between int and String

Convert int to String

Conversion mode
Method 1: add an empty string directly after the number

public class Demo {
    public static void main(String[] args) {
       int s=30;
       String s1=s+"";
        System.out.println(s1);
        }
    }

Method 2: use the String class static method valueOf()

public class Demo {
    public static void main(String[] args) {
       int s=30;
      String s1= String.valueOf(s);
        System.out.println(s1);
        }
    }

Convert String to int

Conversion mode
Method 1: first convert the string number to Integer, and then call the valueOf() method

public class Demo {
    public static void main(String[] args) {
        String s="30";
        Integer s1=Integer.valueOf(s);
        System.out.println(s1);
    }
    }

Method 2: convert through Integer static method parselnt()

public class Demo {
    public static void main(String[] args) {
        String s="30";
      int s1=Integer.parseInt(s);
        System.out.println(s1);
    }
    }

Automatic packing and unpacking

Automatic packing ------------ convert the basic data type to the corresponding packing type

Automatic unpacking ------------ convert the packaging type to the corresponding basic data type

example:

public class Demo{
    public static void main(String[] args) {
        //Packing: convert the basic data type to the corresponding packing type
        //100 is the basic type, which is converted to the corresponding wrapper class type (i) by calling the valueOf method of Integer
        Integer i = Integer.valueOf(100);//Packing: Integer.valueOf(100);
        Integer ii=100;//Automatic packing
        
        //Unpacking: convert the packaging type to the corresponding basic data type
        //ii+=200;
        //Unpacking: ii.intValue()
     //  ii= ii.intValue()+200;// Automatic packing
        ii+=200;//Automatic unpacking and automatic packing are implied inside
        System.out.println(ii);
    }
}

Date class

Date class

Date represents a specific time, accurate to milliseconds

Construction method

Method namedescribe
public Date()Allocate a Date object and initialize it so that it represents the time it is allocated, accurate to milliseconds
public Date(long date)Allocates a Date object and initializes it to represent the specified number of milliseconds from the standard base time

example:

public class Demo {
    public static void main(String[] args) {
        Date time=new Date();//Allocate a Date object and initialize it so that it represents the time it is allocated, accurate to milliseconds
        System.out.println(time);

        long date=1000*60*60;//Allocates a Date object and initializes it to represent the specified number of milliseconds from the standard base time 
        Date time1=new Date(date);
        System.out.println(time1);
    }
    }

Operation results:
Thu Sep 23 19:49:35 CST 2021
Thu Jan 01 09:00:00 CST 1970

Common methods:

Method namedescribe
public long getTime()Gets the millisecond value of the date object from 00:00:00, January 1, 1970 to the present
public void setTime(long time)Set the time and give the value of milliseconds

example:

import java.util.Date;
public class Demo {
    public static void main(String[] args) {
        Date time=new Date();
        System.out.println("1970 The millisecond value from January 1, to now is:"+time.getTime());

//The time distance from 1970-1-01 00:00:00.000 to the current time is obtained. The type is long.
        long time1=System.currentTimeMillis();//Set the time and give the value of milliseconds
        time.setTime(time1);
        System.out.println(time);
    }
    }
Operation results:
1970 The millisecond value from January 1, 2003 to now is 1632398241803
Thu Sep 23 19:57:21 CST 2021

SimpleDateFormat class

summary
The SimpleDateFormat class is a concrete class for formatting and parsing dates in a locale sensitive manner

Construction method

Method namedescribe
public SimpleDateFormatConstruct a SimpleDateFormat, using the default mode and date format
public SimpleDateFormat(String pattern)Construct a SimpleDateFormat using the given pattern and the default date format

common method

Format (from Date to String)
public final String format(Date date): formats the date into a date / time string

example:

public class Demo {
    public static void main(String[] args) {
        Date time=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        String s=sdf.format(time);
        System.out.println(s);
    }
    }
Operation results:
2021-09-23  20:03:54

Parsing (from String to Date)

public Date parse(String source): parses text from the beginning of a given string to generate a date

example:

public class Demo {
    public static void main(String[] args) throws ParseException {
     String time="2021-9-23 08:05:44";
     SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(time);
        System.out.println(date);
    }
    }
    Operation results:
    Thu Sep 23 08:05:44 CST 2021

Calendar Class

summary

Calendar provides some methods for the conversion between a specific moment and a set of calendar fields, and some methods for manipulating calendar fields

Calendar provides a class method, getInstance, to get commonly useful objects of this type. This method returns a calendar object.
Its calendar field has been initialized with the current date and time: Calendar rightNow =Calendar.getInstance();

Calendar class is an abstract class, which cannot be instantiated, but can be instantiated through its subclasses; You can provide a conversion method between a specific time and a calendar field
common method

Method namedescribe
public int get(int field)Returns the value of the given calendar field
public abstract void add(int field, int amount)Add or subtract the specified amount of time from the given calendar field according to the rules of the calendar
public final void set(int year,int month,int date)Set the month, year and day of the current calendar

example:

public class Demo {
    public static void main(String[] args) throws ParseException {
        //Get calendar class object
        Calendar c = Calendar.getInstance();

        //public int get(int field): returns the value of the given calendar field
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int date = c.get(Calendar.DATE);
        System.out.println(year + "year" + month + "month" + date + "day");

        //Add or subtract the specified amount of time from the given calendar field according to the rules of the calendar
        c.add(Calendar.YEAR,-2);//Minus 2 years
        System.out.println(c);

        //Set the month, year and day of the current calendar
        c.set(2021,9,23);
        System.out.println(c);
    }
    }

Posted by liquefi on Sun, 26 Sep 2021 19:18:24 -0700