API官方文档
Date
package org.forten.api.date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateTest003 { public static void main(String[] args) throws ParseException { String dateStr = "2019-01-01"; String dateTimeStr = "2019-10-01 09:10:22"; //把日期格式的字符串转型为Java.util.Date类型的对象 //使用SimpleDateFormat对象的parse(String)方法,此方法需要处理ParseException //yy:两位的年份,小于50表示20XX年,大于50表示19XX年 //yyyy:四位的年份 //MM:月份(1~12) //HH:24小时制(0~23) //hh:12小时制(1~12) //mm:分钟(0~59) //ss:秒 //SSS:毫秒 SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd"); Date date1 = sdf1.parse(dateStr); System.out.println(date1); SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date dateTime1 = sdf2.parse(dateTimeStr); System.out.println(dateTime1); //包java.util.Date类型的日期对象转型为某种日期格式的字符串 //使用SimpleDateFormat对象的format(Date)方法 SimpleDateFormat sdf3 = new SimpleDateFormat("MM/dd/yyyy"); String dateStr2 = sdf3.format(date1); System.out.println(dateStr2); SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); String dateTimeStr2 = sdf4.format(dateTime1); System.out.println(dateTimeStr2); } }
package org.forten.api.date; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoField; public class LocalDateTest001 { public static void main(String[] args) { //得到当前日期 LocalDate currentDate=LocalDate.now(); System.ou.println(currentDate); //指定年月日,得到具体对应的日期对象 LocalDate date1=LocalDate.of(2020,8,25); System.out.println(date1); //通过对日期字符串的解析,得到日期对象 LocalDate date2=LocalDate.parse("2020-02-18"); System.out.println(date2); LocalDate date3 = LocalDate.parse("20200218", DateTimeFormatter.ofPattern("yyyyMMdd")); System.out.println(date3); //使用minus()方法在原日期上减去相应单位的数量,并返回一个新的LocalDate对象 //LocalDate date4=date1.minus(41,ChronoUnit.Years); LocalDate date4 = date1.minusYears(41); System.out.println(date1); System.out.println(date4); //LocalDate date5=date2.minus(30,Chronounit.DAYS); LocalDate date5 = date2.plusDays(30); System.out.println(date2); System.out.println(date5); int year = date5.get(ChronoField.YEAR); System.out.println(year); int monthValue = date5.getMonthValue(); System.out.println(monthValue); //日期转字符串 LocalDate date6 = LocalDate.of(1985, 9, 1); String studentDay = date6.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")); System.out.println(studentDay); LocalDateTime dateTime1 = date6.atTime(7, 30, 0); studentDay = dateTime1.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss")); System.out.println(studentDay); LocalDateTime dateTime2 = LocalDateTime.parse(studentDay, DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss")); System.out.println(dateTime2); } }
package org.forten.api.packagetype; public class BooleanTest001 { public static void main(String[] args) { Boolean b1 = Boolean.valueOf(true); Boolean b2 = Boolean.valueOf(true); System.out.println(b1 == b2);// b1 = Boolean.valueOf(false); b2 = Boolean.valueOf(false); System.out.println(b1 == b2);// } }
package org.forten.api.packagetype; public class IntegerTest001 { public static void main(String[] args) { //Integer.valueOf(int) //得到int值对应的Intger对象 Integer i1 = Integer.valueOf(-128); Integer i2 = Integer.valueOf(-128); System.out.println(i1 == i2);// true Integer i3 = Integer.valueOf(-129); Integer i4 = Integer.valueOf(-129); System.out.println(i3 == i4); //通过intValue()得到与Integer相应的int类型的值 //XXXValue()可以返回XXX基本数据类型的值 //不同的数字包装类都包含这些方法 int int1 = i1.intValue(); byte byte1 = i1.byteValue(); double double1 = i1.doubleValue(); long long1 = i1.longValue(); //integer.valueOf(String,int) //String:待转换的数字格式字符串 //int:进制数 Integer i6 = Integer.valueOf("ff", 16); System.out.println(i6); i6 = Integer.valueOf("1000", 2); System.out.println(i6); i6 = Integer.valueOf("15", 22); System.out.println(i6); i6 = Integer.valueOf("1k", 22); System.out.println(i6); int int2 = 20; System.out.println(Integer.toBinaryString(int2)); System.out.println(Integer.toOctalString(int2)); System.out.println(Integer.toHexString(int2)); System.out.println(Integer.toString(int2, 22)); int int3 = -2147483648; long long2 = Integer.toUnsignedLong(int3); System.out.println(long2); //String与Integer的转换 Integer i7 = Integer.parseInt("-123"); System.out.println(i7); i7 = Integer.parseInt("1e",16); System.out.println(i7); } }
-
package org.forten.api.packagetype; public class IntegerTest002 { public static void main(String[] args) { //Java5以前包装类对象和相应的基本数据类型 //必须以显示的方式调用进行转换 Integer xxx = Integer.valueOf(100); int yyy = xxx.intValue(); //Java5后引入了自动装箱/拆箱操作,简化了之间的方法调用 //次方法特性是为语法糖 Integer i1 = 100;// 自动装箱 int i2 = i1;// 自动拆箱 System.out.println(i1); System.out.println(i2); } }
package org.forten.api.sort; public class Apple implements Comparable<Apple> { private String name; private int heavy; private int price; public Apple(String name, int heavy, int price) { super(); this.name = name; this.heavy = heavy; this.price = price; } public String getName() { return name; } public int getHeavy() { return heavy; } public int getPrice() { return price; } @Override public String toString() { return "Apple [name=" + name + ", heavy=" + heavy + ", price=" + price + "]"; } @Override public int compareTo(Apple that) { if (this.price > that.price) { return 1; } else if (this.price < that.price) { return -1; } else { return 0; } //优点:简洁 //缺点:在数字比较运算过程中有可能会出现数据越界的情况 } }
package org.forten.api.sort; import java.util.Comparator; public class HeavyComparator implements Comparator<Apple> { @Override public int compare(Apple o1, Apple o2) { if (o1.getHeavy() > o2.getHeavy()) { return 1; } else if (o1.getHeavy() < o2.getHeavy()) { return -1; } else { return 0; } } }
package org.forten.api.sort; import java.util.Arrays; import java.util.Comparator; //一般的外部类:很多类中都会使用这个外部类提供的逻辑 //非静态内部类:本类中多个方法共享的逻辑,在内部类中要访问外部类的非静态成员 //静态内部类:在本类中多个方法共享的逻辑,在内部类中不用 访问外部类的非静态成员 //匿名内部类:只有在方法参数上使用一次的逻辑(可以用Java8的Lambda表达式替代) public class SortTest001 { public static void main(String[] args) { Integer[] intArr = { 28, 38, 122, 12, -38, 4, 0, 1, 32, 62, 91 }; Arrays.sort(intArr); System.out.println(Arrays.toString(intArr)); Apple a1 = new Apple("国光", 30, 3); Apple a2 = new Apple("蛇果", 100, 30); Apple a3 = new Apple("黄香蕉", 150, 15); Apple a4 = new Apple("红富士", 120, 10); Apple a5 = new Apple("塑料苹果", 10, 8); Apple a6 = new Apple("红香蕉", 160, 15); Apple[] appleArr = { a1, a2, a3, a4, a5, a6 }; Arrays.sort(appleArr); System.out.println(Arrays.toString(appleArr)); Arrays.sort(appleArr, new HeavyComparator()); System.out.println(Arrays.toString(appleArr)); Arrays.sort(appleArr, new SortTest001().new ReverseHeavyComparator()); System.out.println(Arrays.toString(appleArr)); Arrays.sort(appleArr, new SortTest001.ReversePriceComparator()); System.out.println(Arrays.toString(appleArr)); //匿名内部类 //new(implements|extends)interface|abstractclass Arrays.sort(appleArr, new Comparator<Apple>() { @Override public int compare(Apple o1, Apple o2) { if (o1.getPrice() > o2.getPrice()) { return 1; } else if (o1.getPrice() < o2.getPrice()) { return -1; } else { if (o1.getHeavy() > o2.getHeavy()) { return -1; } else if (o1.getHeavy() < o2.getHeavy()) { return 1; } else { return 0; } } } }); System.out.println(Arrays.toString(appleArr)); Arrays.sort(appleArr, (o1, o2) -> { if (o1.getPrice() > o2.getPrice()) { return 1; } else if (o1.getPrice() < o2.getPrice()) { return -1; } else { if (o1.getHeavy() > o2.getHeavy()) { return -1; } else if (o1.getHeavy() < o2.getHeavy()) { return 1; } else { return 0; } } }); System.out.println(Arrays.toString(appleArr)); Arrays.sort(appleArr, SortTest001::compareWithPriceAndReverseWeight); System.out.println(Arrays.toString(appleArr)); } private static int compareWithPriceAndReverseWeight(Apple o1, Apple o2) { if (o1.getPrice() > o2.getPrice()) { return 1; } else if (o1.getPrice() < o2.getPrice()) { return -1; } else { if (o1.getHeavy() > o2.getHeavy()) { return -1; } else if (o1.getHeavy() < o2.getHeavy()) { return 1; } else { return 0; } } } private static class ReversePriceComparator implements Comparator<Apple> { @Override public int compare(Apple o1, Apple o2) { if (o1.getPrice() > o2.getPrice()) { return -1; } else if (o1.getPrice() < o2.getPrice()) { return 1; } else { return 0; } } } private class ReverseHeavyComparator implements Comparator<Apple> { @Override public int compare(Apple o1, Apple o2) { if (o1.getHeavy() > o2.getHeavy()) { return -1; } else if (o1.getHeavy() < o2.getHeavy()) { return 1; } else { return 0; } } } } Arrays.sort(appleArr, new Comparator<Apple>() { @Override public int compare(Apple o1, Apple o2) { if (o1.getPrice() > o2.getPrice()) { return 1; } else if (o1.getPrice() < o2.getPrice()) { return -1; } else { if (o1.getHeavy() > o2.getHeavy()) { return -1; } else if (o1.getHeavy() < o2.getHeavy()) { return 1; } else { return 0; } } } }); System.out.println(Arrays.toString(appleArr)); Arrays.sort(appleArr, (o1, o2) -> { if (o1.getPrice() > o2.getPrice()) { return 1; } else if (o1.getPrice() < o2.getPrice()) { return -1; } else { if (o1.getHeavy() > o2.getHeavy()) { return -1; } else if (o1.getHeavy() < o2.getHeavy()) { return 1; } else { return 0; } } }); System.out.println(Arrays.toString(appleArr)); Arrays.sort(appleArr, SortTest001::compareWithPriceAndReverseWeight); System.out.println(Arrays.toString(appleArr)); } private static int compareWithPriceAndReverseWeight(Apple o1, Apple o2) { if (o1.getPrice() > o2.getPrice()) { return 1; } else if (o1.getPrice() < o2.getPrice()) { return -1; } else { if (o1.getHeavy() > o2.getHeavy()) { return -1; } else if (o1.getHeavy() < o2.getHeavy()) { return 1; } else { return 0; } } } private static class ReversePriceComparator implements Comparator<Apple> { @Override public int compare(Apple o1, Apple o2) { if (o1.getPrice() > o2.getPrice()) { return -1; } else if (o1.getPrice() < o2.getPrice()) { return 1; } else { return 0; } } } private class ReverseHeavyComparator implements Comparator<Apple> { @Override public int compare(Apple o1, Apple o2) { if (o1.getHeavy() > o2.getHeavy()) { return -1; } else if (o1.getHeavy() < o2.getHeavy()) { return 1; } else { return 0; } } } }