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
-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.
long variable name = System.currentTimeMillis ();
:
System.currentTimeMilli returns the current time expressed in ** milliseconds. ** **
・ 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.
** If it is a Date type variable, it can be determined that the content is date and time information **.
ʻimport` the java.date.util class
Date variable name = new Date ();
Date variable name = new Date (long value argument);
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 ③
Sat Mar 03 11:41:38 JST 2018//①
1520044898396//②
Sat Mar 03 11:41:38 JST 2018//③
After all, it is difficult to read because it uses a long value ...
Long type data obtained by Date
class can be converted to int type data by Calender
class.
Import java.util.Calendar;
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
ʻ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);
Instance name.setTime (Date type argument);
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 ②
}
2018//①
Mon Feb 20 10:10:10 JST 1995//②
Long type data obtained by Date
class can be converted to String type data by SimpleDateFormat
class.
java.text.SimpleDateFormat;
Import class
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.
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.
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);
}
2018/03/03 15:59:07
Fri Jan 20 10:10:10 JST 1995
When defining a class, if you do not specify the parent class with ʻextend, it means that the parent class inherits
java.lang.Object`.
· ʻEquals (): Find out if you are the same as an instance -
ToString ()`: Returns the contents of your own string
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);
}
}
Java14.Hero@70dea4e
Hello
Due to polymorphism, all objects can be roughly regarded as ʻObject, so ʻObject
can be specified when ** argument = anything **.
Model name @ alphanumeric
//When outputting a Hero instance
Java14.Hero@70dea4e
@Override
public String toString(){
return "Name is"+this.name ...
}
It is necessary to ** override ** the output method as described above.
@Override
means to override.
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
.
Exactly the same state
Hero h1 = new Hero;
Hero h2 = h1;
In this case, h1 and h2 are equal.
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.
However, since the default ʻequals` is an equal value judgment, it is necessary to modify it to an equivalence judgment and use it.
//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;
}
}
Same content
Classes for basic data types such as int
All methods are written in static
, so you can use the wrapper class without instantiating it.
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.
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
15
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 ...
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