2017/02/09 Added time and date
Those who aim for javaGold Those who understand javaSilver
Focus on the parts that can be reached where it is itchy Inner class or anonymous class Since it also serves as a memorandum, please forgive comments and typographical errors in variable names (laugh) Will be updated from time to time static
static_.java
public class static_ {
int x;
static int y;
//Accessing static instances from dynamic methods
void doA(){
x=1; //OK
y=2; //OK
}
static void doB(){
// x=1; //NG
y=2; //OK
}
}
final
final_.rjava
//final class
//Not inheritable
final class final_{
//final field
//Cannot be reassigned
final int x = 0;
//final method
//Cannot be overridden
final void doIt() {
//final local variable
//Cannot be reassigned
final int y = 1;
//Below, compilation error
/*
x = 10;
y = 10;
}
}
class temp extends finalMethod {
@Override
public void doIt(){
}
*/
}
}
abstract
abstract_.rjava
abstract class A{
void X (){};
abstract void y();
// void Z(); //Be sure to add abstract
}
/*NG abstract class must be implemented firmly
public class B extends A{
}
*/
//OK
abstract class C extends A{
void y(){
System.out.println("###");
}
}
naibu.java
public class naibu {
//Below, nested class
//① static menba class
static class X {}
//② Member class
private class Y{}
void doIt(){
//③ Local class
class Z{}
}
//Calling an inner class
public static void main(String args[]){
Outer.Inner in = new Outer().new Inner();
in.doIt();
Outer.Inner.Inner2 in2 = new Outer().new Inner().new Inner2();
in2.doIt();
}
}
class Outer{
public class Inner{
public void doIt(){
System.out.println("Outer->Inner");
}
public class Inner2{
public void doIt(){
System.out.println("Outer->Inner->Inner2");
}
}
}
}
interface_.java
public class interface_ {
public static void main(String[] args){
//static method
Bar bar = new Bar();
Foo.doA();
// Bar.doA();//NG implementation class → static
// bar.doA();//NG implementation class → static
//default method
bar.doB();
}
}
interface Foo {
//Internally qualified with public static final
int interfaceMenba =10;
public static final int interfaceMenba2 = 10;
//Ordinary abstract method
//Internally qualified with public abstract
void doZ();
//static method
static void doA() {
System.out.println("static");
}
//default method
default void doB() {
System.out.println("default");
}
}
class Bar implements Foo {
public void doZ(){
System.out.println("doZ");
}
}
anonymous.java
public interface anonymous {
public void doIt();
}
class anonymousImpl{
public static void main (String[] args){
//Implemented at the same time as the declaration by the anonymous class(Not running)
anonymous A = new anonymous() {
public void doIt() {
System.out.println("Declaring a class with an anonymous class");
}
};
//Just run it for simplicity
new anonymous(){
public void doIt(){
System.out.println("Execution by anonymous class");
}
}.doIt();
//↑ This is the point
}
}
rekkyo.java
public class rekkyo {
//Nested enums
public enum singou {
GREEN, RED, YELOW
}
public static void main(String[] args) {
//Enumerate all enums
for (singou2 s : singou2.values()) {
System.out.println(s.name() + "#");
}
//Enum declarations and field references
Souldout so = Souldout.bro_hi;
System.out.println(so.getV() + "#" + so);
}
}
//Top-level enums
enum singou2 {
GREEN, RED, YELOW;
//Variables / methods can be placed in enumeration type
private int a;
public int b;
public int add(int a, int b) {
return a + b;
}
}
enum Souldout {
//Enum with constructor ↓
diggy_mo(2), sinnosuke(3), bro_hi(5);
private int nenrei;
//Constructor must be private
// public Souldout(){
// }
private Souldout() {
}
private Souldout(int v) {
this.nenrei = v;
}
public int getV() {
return nenrei * 100;
}
}
//It is possible to implement an interface
interface temp {
void doItIt();
}
enum singou3 implements temp {
GREEN, RED, YELOW;
public void doItIt() {
System.out.println("####");
}
}
//However, class inheritance is not possible
class temp2 {
int a;
}
/*
enum singu4 extends temp2{
}
*/
2017/02/06 update
DATE.java
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAccessor;
import static java.time.temporal.ChronoUnit.DAYS;
import static java.time.temporal.ChronoUnit.MONTHS;
import static java.time.temporal.ChronoUnit.WEEKS;
public class DATE {
public static void main(String[] args){
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//LocalDate / LocalDateTime
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//There are the following methods to create an instance
LocalDate ld = LocalDate.now();
ld = LocalDate.of(2016, Month.MARCH,10); // Month.MARCH ⇔ 2
ld = LocalDate.parse("2016-12-12");
//ld = new LocalDate(); //NG
LocalDateTime ldt = LocalDateTime.now();
//isBefore method
//isAfter method
//System.out.println(ldt.isAfter(ld)); //NG LocalDate and LocalDateTime
//System.out.println(ld.isAfter(ldt)); //NG LocalDateTime and LocalDate
System.out.println(LocalDate.now().isAfter(ld)); //OK
//ldt = LocalDateTime.from(ld); //Time information is required to change from NG LocalDate to LocalDateTime!
ld = LocalDate.from(ldt); //OK LocalDateTime to LocalDate do not require time information
System.out.println(ldt.plus(5,MONTHS)
+ "#" + ldt.minus(365,DAYS)
+ "#" + ldt.until(ldt.plus(10,WEEKS),WEEKS));
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Temporal ←(Inheritance)― TemporalAccessor ←(Implementation)― ChronoUnit
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
TemporalAccessor ta =ChronoUnit.DAYS.addTo(LocalDate.now(),10);
Temporal t =ChronoUnit.DAYS.addTo(LocalDate.now(),10);
//LocalDateTime and LocalDateTime are not compatible
//long days = ChronoUnit.DAYS.between(LocalDateTime.now(),LocalDate.now().plus(5,DAYS)); //NG
long days = ChronoUnit.DAYS.between(LocalDate.now(),LocalDate.now().plus(5,DAYS)); //OK
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Period Represents the period of the date
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Period p = Period.between(LocalDate.now(),LocalDate.now().plus(5,WEEKS));
//See the contents of Period
System.out.println(p.getDays());
//You can also add / remove periods to Period
Period newP = p.minusDays(10);
//Can be added or subtracted from Period
LocalDate ld2 =(LocalDate) p.addTo(ld);
ld2 =(LocalDate) p.subtractFrom(ld);
//Addition / subtraction between Periods is also possible
Period p2 = p.plus(Period.ofDays(5));
Period p3 = p.minus(Period.ofDays(5));
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Duration Represents the duration of a date
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Duration d = Duration.between(LocalDateTime.now(),LocalDateTime.now().plusDays(1));
System.out.println(d);
d = Duration.of(1,ChronoUnit.MINUTES);
System.out.println("nanos" + d.getNano());
System.out.println("seconds" + d.getSeconds());
DateTimeFormatter dtf_bid = DateTimeFormatter.BASIC_ISO_DATE;
DateTimeFormatter dtf_idt = DateTimeFormatter.ISO_DATE_TIME;
DateTimeFormatter dtf_ii = DateTimeFormatter.ISO_INSTANT;
DateTimeFormatter dtf_original = DateTimeFormatter.ofPattern("yyyy_MM_dd_hh_mm_ss"); //Define your own display format
System.out.println(
dtf_bid.format(LocalDateTime.now())
+ "#" +dtf_idt.format(LocalDateTime.now())
//+ "#" + dtf_ii.format(LocalDateTime.now()) //NG
+ "#" + dtf_original.format(LocalDateTime.now()));
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Instant A class with cumulative seconds, milliseconds, and nanoseconds from a certain point in time
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//What is a certain point ↓
System.out.println(Instant.EPOCH); //January 1, 1970 0:00:00
//Instance acquisition
Instant i1 = Instant.ofEpochMilli(51455414);
Instant i2 = Instant.ofEpochSecond(51455414);
Instant i3 = Instant.ofEpochSecond(51455414,2515); //The second argument is milliseconds
//Instant instant = Instant.from(LocalTime.now()); //NG Unsupported
//Acquisition of seconds, milliseconds, and nanoseconds
System.out.println(Instant.now().getEpochSecond()); //get
System.out.println(Instant.now().toEpochMilli()); //to (Why is this all to?)
System.out.println(Instant.now().getNano()); //get
System.out.println(Instant.ofEpochMilli(500000));
//From various Temporal to Instant
//From OffsetDateTime, toInstant()Get in
OffsetDateTime odt = OffsetDateTime.now();
System.out.println(odt.toInstant());
//From ZonedDateTime, toInstant()Get in
System.out.println(ZonedDateTime.now(ZoneId.systemDefault()).toInstant());
//LocalDate does not have offset, so you need to set it when calculating instant
System.out.println(LocalDateTime.now().toInstant(ZoneOffset.UTC));
}
}
Recommended Posts