编程知识 cdmana.com

Java common API

Arrays Practical functions

public class ArraysFunc {
    public static void main(String[] args) {
        int[] i = new int[7];
        int[] j = new int[10];
        Arrays.fill(i, 47);// fill , Optional range 
        Arrays.fill(j, 99);
        System.out.println("i="+Arrays.toString(i));// Convert to string printing 
        System.out.println("j="+Arrays.toString(j));
        System.arraycopy(i,0,j,0,i.length);// Copy , Optional range 
        System.out.println("j="+Arrays.toString(j));
        System.out.println(Arrays.equals(i,j));// Compare 
        i = new int[]{4,1,2,3,5};
        System.out.println(Arrays.binarySearch(i, 4));// Two points search , unsorted , Optional range 
        List intList=Arrays.asList(i); //intList One of them Integer Array type objects , The entire array is stored as an element 
        for(Object o:intList){// Just one element 
            System.out.println(o.toString());
        }
        Integer ob[]={4,1,2,3,5};
        List<Integer> objList=Arrays.asList(ob); // Every element in the array is used as list An element in 
        objList.add(7); // Can not add 
        for(int a:objList){
            System.out.println(a);
        }
    }
}

Scanner

  • Text scanner , You can use regular expressions ( As a closing sign ) Parsing primitive types and strings
  • By default, spaces and newlines are used as separators
  • Common constructors string,stream,file
  • The console input is passed in as an input stream Scanner Constructor for , Then this method will block before there is input
public class UseScanner {
    public static void main(String[] args) {
        String input = "1 fish 2 fish red fish blue fish";
        Scanner s = new Scanner(input);
        s.useDelimiter("s*fishs*");
        while (s.hasNext()) {
            System.out.println(s.next());
        }
        System.out.println(" end ");// Use System.in structure , The program will block until while In circulation , I can't get out 
        s.close();
    }
}

System

public class UseSystem {
    public static void main(String[] args) throws IOException {
        //System.arraycopy();// Copy the array 
        System.out.println(System.currentTimeMillis());//ms Time stamp 
        System.getProperties().list(System.out);// Get the current system properties 
        System.out.println(System.getProperty("user.dir"));// Get the project path 
        Map<String,String> map = System.getenv();// An immutable string mapping view of the current system environment 
        for (String key :
                map.keySet()) {
            System.out.println("key:"+key+" value:"+map.get(key));
        }
    }
}

LocalDateTime

public class DateTime {
    public static void main(String[] args) {
        System.out.println(LocalDate.now()); // system date 
        System.out.println(LocalDate.of(1997,3,20)); // A specific day, month, year of
        System.out.println(LocalDate.parse("1997-03-20")); // A specific day, month, year parse, You have to use -
        System.out.println(LocalTime.parse("06:30")); // Specific time, minute, second parse, You have to use :
        System.out.println(LocalDateTime.parse("2015-02-20T06:30:00"));// There must be T
        System.out.println(LocalDateTime.now());// Default band T
        System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd")));// Can't modify the object , Return to new 
        Period period = Period.between(LocalDate.of(2019, 10, 10), LocalDate.of(2020, 10, 20));
        System.out.println(" Differ in : "+period.getYears()+"  Differ in  :"+period.getMonths() +"  By day :"+period.getDays());
 // Differ in : 1  Differ in  :0  By day :10
        Duration duration = Duration.between(LocalTime.of(10, 30, 00), LocalTime.of(20, 00, 00));
        System.out.println(" Phase difference : "+duration.toHours()+"  The difference is  :"+duration.toMinutes() +"  Second difference :"+duration.toMillis());
 // Phase difference : 9  The difference is  :570  Second difference :34200000
        Timestamp timestamp = Timestamp.valueOf(LocalDateTime.now());//LocalTime Turn time stamp 
        System.out.println(timestamp.getTime());// Millisecond time stamp 
        System.out.println(timestamp.toLocalDateTime().format(DateTimeFormatter.ofPattern("yyyy/MM/dd")));// Time stamping LocalTime
 }
}
}
  • Time stamp : from 1970 year 1 month 1 Japan (UTC/GMT At midnight ) The number of seconds to start
  • Why use timestamps : In the database system , Different databases have different interpretations of time types , Such as Oracle Medium date and mysql Medium date It's not directly compatible with transformations , To achieve cross platform , Record the time as unix Time stamp

版权声明
本文为[Virtuous fish]所创,转载请带上原文链接,感谢

Tags java common api
Scroll to Top