[JAVA] About standard classes

How to handle dates

The method of handling the date is as follows -Stored in a long type variable -Use java.date.util class ・ Use Calendar class -Use of SimpleDateFormat class

Store in long type variable

Feature

-For long type variables, date and time information is expressed in the number of milliseconds (1/1000) that have passed, based on 1970/1/1 00:00:00 (epoch). -Long type variables are easy for computers to understand because they are a list of numerical values such as 11223344.

** How to find out the current date and time **

long variable name = System.currentTimeMillis ();: System.currentTimeMilli returns the current time expressed in ** milliseconds. ** **

problem

・ Human cannot understand the enumeration of numerical values such as "11223344" ... -Since long type data is used for other purposes, it is unknown whether the data contained in it is a date.

Stored in java.date.util package

Feature

** If it is a Date type variable, it can be determined that the content is date and time information **.

how to use

"Import class"

ʻimport` the java.date.util class

"Create a Date instance with the current date and time"

Date variable name = new Date ();

"Create a data instance with the date and time of the long value argument"

Date variable name = new Date (long value argument);

Example of use

Date now = new Date();//(1) Create a Date instance and get the current date and time.
System.out.println(now);//Output result ①
long nowTime = now.getTime();//(2) Use the getTime method of the Date instance to get the current date and time of the long value.
System.out.println(nowTime);//Output result ②
Date past = new Date(nowTime);//③ Substitute a long value to create a new data instance
System.out.println(past);//Output result ③

"Output result"

Sat Mar 03 11:41:38 JST 2018//①
1520044898396//②
Sat Mar 03 11:41:38 JST 2018//③

problem

After all, it is difficult to read because it uses a long value ...

Use Calendar class

Feature

Long type data obtained by Date class can be converted to int type data by Calender class.

how to use

"Use Calender class to generate Date type data with int type argument"

Import java.util.Calendar;

"Use Calender class to generate Date type data with int type argument"

Calendar instance name = Calendar.getInstance (); // Create an instance of the Calendar class Instance name.set (year, month, day, hour, minute, second); // Enter date and time information in int type for instance * Month is set from 0 to 11! !! Or Instance name.set (Calendar. ~, Value); // Date variable name = c.getTime (); // Extract Date type data

(How to retrieve int type data from Calendar class)

ʻInt year = Calender class variable .get (Calendar.YEAR); ʻInt month = Calender class variable .get (Calendar.MONTH); ʻInt day = Calender class variable.get (Calendar.DAY_OF_MONTH); ʻInt hour = Calender class variable .get (Calendar.HOUR); ʻInt minute = Calender class variable .get (Calendar.MINUTE); ʻInt second = Calender class variable .get (Calendar.SECOND);

"How to put Date type data in Calendar class"

Instance name.setTime (Date type argument);

Example of use

	public static void main(String[] args) {
		//Create an instance
		Date now = new Date();//Create a now instance of Date class and get the current date and time
		Calendar calendar = Calendar.getInstance();//Create a calendar instance of the Calendar class
		
		//(1) Extract only the year as an int type from the calendar instance
		calendar.setTime(now);//① Substitute the now instance (current date and time) for the calender instance
		int year = calendar.get(Calendar.YEAR);//① Take out only the year from the calender class
		System.out.println(year);//Output result ①
		
		//② Set the year and month with int type
		calendar.set(1995,1,20,10,10,10);//(2) Set the year and month as an int type in the calender instance
		Date past = calendar.getTime();//(2) Assign the set date to the Date type variable past
		System.out.println(past);//Output result ②
	}

"Output result"

2018//①
Mon Feb 20 10:10:10 JST 1995//②

Use of SimpleDateFormat class

Feature

Long type data obtained by Date class can be converted to String type data by SimpleDateFormat class.

how to use

"Import class"

java.text.SimpleDateFormat; Import class

"Generate String type from Date type"

SimpleDateFormat variable name = new SimpleDateFormat (format string); // create an instance String variable name = SimpleDateFormat instance name.format (Date type variable); // Convert the type from Date to String with the format method of the SimpleDateFormat class.

"Generate Date type from String type"

SimpleDateFormat variable name = new SimpleDateFormat (format string); // Create an instance of SimpleDateFormat class Date Date type variable name = SimpleDateFormat instance name.parse (character string); // Convert the type from String to Date with the parse method of the SimpleDateFormat class.

Example of use

	public static void main(String[] args) throws Exception {
		//Create an instance
		Date now = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

		//Date type → String type
		String date = format.format(now);

		//String type → Date type
		Date past = format.parse("1995/01/20 10:10:10");

		//output
		System.out.println(date);
		System.out.println(past);
	}

"Output result"

2018/03/03 15:59:07
Fri Jan 20 10:10:10 JST 1995

Implicit inheritance

** Thinking **

When defining a class, if you do not specify the parent class with ʻextend, it means that the parent class inherits java.lang.Object`.

** Contents of java.lang.Object **

· ʻEquals (): Find out if you are the same as an instance -ToString ()`: Returns the contents of your own string

** Benefits of implicit inheritance **

① Object can be used as an argument

"Example"

	public static void main(String[] args) {
		Object[] o = new Object[2];
		o[0] = new Hero();
		o[1] = "Hello";
		for(Object ox:o) {
			System.out.println(ox);
		}
	}
"Output result"
Java14.Hero@70dea4e
Hello

Due to polymorphism, all objects can be roughly regarded as ʻObject, so ʻObject can be specified when ** argument = anything **.

(2) All classes can be equipped with the minimum required methods.

** About toString **

"Default"

Model name @ alphanumeric

//When outputting a Hero instance
Java14.Hero@70dea4e

"If you want to change"

@Override
public String toString(){
 return "Name is"+this.name ...
}

It is necessary to ** override ** the output method as described above. @Override means to override.

** About equals **

Roughly speaking, it is judged whether they are the same. Whether they are the same or not is usually judged by ==, but equals makes a slightly different equivalence judgment.

"Equal value =="

Exactly the same state

Hero h1 = new Hero;
Hero h2 = h1;

In this case, h1 and h2 are equal.

"Equivalent equals"

The same content

Hero h1 = new Hero("Yoshihiko")
Hero h2 = new Hero("Yoshihiko")

In this case, h1 and h2 are separate instances, but they are equivalent because they have the same contents.

"how to use"

However, since the default ʻequals` is an equal value judgment, it is necessary to modify it to an equivalence judgment and use it.

"Example"

//main class
	public static void main(String[] args) {
		Hero hero1 = new Hero();
		hero1.name = "Yoshihiko";
		hero1.hp = 100;

		Hero hero2 = new Hero();
		hero2.name = "Yoshihiko";
		hero2.hp = 100;

		if (hero1.equals(hero2) == true) {
			System.out.println("Same content");
		} else {
			System.out.println("It's different");
		}
	}
//Hero class
public class Hero {
	String name;
	int hp;
	
    //Rewriting equals
    @Override
	public boolean equals(Object o) {
		if (this == o) {//True if equal values
			return true;
		}
		if (o instanceof Hero) {//In the case of the same class or a parent-child relationship class
			Hero h = (Hero) o;//Cast-convert the variable o of the Object class to the variable h of the Hero class and assign it to the variable h of the Hero type.
			if (this.name.equals(h.name)) {//Equivalent if the names are equal
				return true;
			}
		}
		return false;
	}
}

"Output result"

Same content

Wrapper class

What is a wrapper class?

Classes for basic data types such as int

Benefits of wrapper classes

"Methods available"

All methods are written in static, so you can use the wrapper class without instantiating it.

"Can be treated as an instance"

Since there are some APIs that cannot be handled as they are as basic data types, we purposely instantiate them once and give them numerical values.

"Example of use"
		int i1 = 15;//Substitute 15 for int type variable i1
		Integer i2 = Integer.valueOf(i1);//i2 instance generation
		System.out.println(i2);

valueOf: A method that returns an Integer instance that represents the specified int value

"Output result"
15

Basic data type and wrapper class conversion

Normally

		int i1 = 15;//Substitute 15 for int type variable i1
		Integer i2 = Integer.valueOf(i1);//i2 instance generation ・ Convert from int type to Integer type
		int i3 = i2.intValue();//Convert from Integer type to int type

intValue: A method that returns an Integer value as an int

With this, you need to use the method every time you convert the data ...

"AutoBoxing / AutoUnboxing function"

Wrapper class type ⇆ When converting the basic data type, valueOf and ʻintValue` are automatically performed. This AutoBoxing / AutoUnboxing function is provided by default.

		int i1 = 15;//Substitute 15 for int type variable i1
		Integer i2 = i1;//Automatic conversion from int type to Integer type
		int i3 = i2;//Automatic conversion from Integer type to int type

Recommended Posts

About standard classes
About classes and instances
[Java] About anonymous classes
About classes and instances (evolution)
Consideration about classes and instances
About =
About abstract classes in java
About Ruby classes and instances
About Kotlin
About attr_accessor
About Hinemos
About params
About Rails 6
About form_for
About Spring ③
About enum
About Optional
About hashes
About JitPack
About Dockerfile
About this ()
About devise
About encapsulation
About Docker
About JAVA_HOME
About active_hash
About static
About exceptions
About scope
[Maven] About Maven
About the idea of anonymous classes in Java
About the standard input stream (System.in) once closed