guyanqing 1 year ago
parent
commit
a91c476be4

+ 255 - 0
src/main/java/com/sf/day12/TestApi.java

@@ -0,0 +1,255 @@
+package com.sf.day12;
+
+import org.junit.Test;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatterBuilder;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Locale;
+import java.util.TimeZone;
+
+import static java.time.LocalDateTime.from;
+import static java.time.LocalTime.now;
+
+public class TestApi {
+    @Test
+    public void t1(){
+      String str = new String("我爱学习");
+      str += "java";
+        System.out.println(str);
+
+        StringBuffer stringBuffer = new StringBuffer("我要吃饭");
+        stringBuffer.append("米饭");
+        System.out.println(stringBuffer);
+
+        System.out.println("我爱打台球" + "中八");
+
+    }
+
+    @Test
+    public void t2(){
+       StringBuffer  stringBuffer  = new StringBuffer("userName=admin");
+       System.out.println(stringBuffer);
+//       stringBuffer.delete(1,4);
+//       System.out.println(stringBuffer);   //uName=admin
+//        stringBuffer.deleteCharAt(0);
+//        System.out.println(stringBuffer);
+//        char c = stringBuffer.charAt(2);
+//        System.out.println(c);
+//        String str = new String("password");
+//        char c1 = str.charAt(1);
+//        System.out.println(c1);
+
+//        stringBuffer.replace(1,3,"guyanqing");
+//        stringBuffer.setCharAt(0,'g');
+//        stringBuffer.insert(0,12);
+//        stringBuffer.length();
+//        stringBuffer.reverse();
+//        int a = stringBuffer.indexOf("a",4);
+//        int a = stringBuffer.lastIndexOf("a",14);
+        String substring = stringBuffer.substring(5);
+        System.out.println(substring);
+//        System.out.println(a);
+        System.out.println(stringBuffer);
+    }
+
+    @Test
+    public void T3(){
+        long startTime = 0L;
+        long endTime = 0L;
+        String text = "";
+        StringBuffer stringBuffer = new StringBuffer();
+        StringBuilder stringBuilder = new StringBuilder();
+
+        /**
+        * stringBuffer
+         */
+        startTime = System.currentTimeMillis();
+        for (int i = 0;i <200000;i++){
+            stringBuffer.append(String.valueOf(i));
+        }
+        endTime = System.currentTimeMillis();
+        System.out.println("buffer  === 所用的时间=="+(endTime-startTime));
+
+
+//        /**
+//         * stringBuilder
+//         */
+        startTime = System.currentTimeMillis();
+        for (int i = 0;i <200000;i++){
+            stringBuilder.append(String.valueOf(i));
+        }
+        endTime = System.currentTimeMillis();
+        System.out.println("Builder  === 所用的时间=="+(endTime-startTime));
+
+
+//        /**
+//         * String
+//         */
+        startTime = System.currentTimeMillis();
+        for (int i = 0;i <200000;i++){
+            text = text+i;
+        }
+        endTime = System.currentTimeMillis();
+        System.out.println("String  === 所用的时间=="+(endTime-startTime));
+    }
+
+    @Test
+    public void t4(){
+        Date date = new Date();
+        System.out.println(date);
+        long l = System.currentTimeMillis();
+        System.out.println(l);
+        Date date1 = new Date();
+        long time = date1.getTime();
+        System.out.println(time);
+        Date date2 = new Date(time);
+
+        //  ======
+        long maxValue = Long.MAX_VALUE;
+        Date date3 = new Date(maxValue);
+        System.out.println(date3);
+    }
+
+    @Test
+    public void t5(){
+        Date date = new Date();
+        System.out.println(date);
+        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
+        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
+        String format = simpleDateFormat.format(date);
+        String format1 = simpleDateFormat1.format(date);
+        System.out.println(format);
+        System.out.println(format1);
+    }
+
+
+    @Test
+    public void t6() throws ParseException {
+//        String str = "2022年06月06日 16时03分14秒 545毫秒  星期四 +0800";
+//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
+//        Date p = simpleDateFormat.parse(str);
+//        System.out.println(p);
+
+        String str1 = "2022年06月06日 16时03分14秒 545毫秒  星期四 +0800";
+        SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
+        Date d = sf.parse(str1);
+        System.out.println(d);
+    }
+
+    @Test
+    public void t7(){
+        Calendar calendar = Calendar.getInstance();
+//        System.out.println(calendar);0
+        int i = calendar.get(Calendar.YEAR);
+        int month = calendar.get(Calendar.MONTH)+1;
+        int day = calendar.get(Calendar.DATE);
+        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
+        int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
+        System.out.println(i);
+        System.out.println(month);
+        System.out.println(day);
+        System.out.println(dayOfMonth);
+        System.out.println(hourOfDay);
+    }
+
+    @Test
+    public void t8(){
+        TimeZone t = TimeZone.getTimeZone("America/LOS_Angeles");
+        Calendar c = Calendar.getInstance();
+        int year = c.get(Calendar.YEAR);
+        int month = c.get(Calendar.MONTH)+1;
+        int day = c.get(Calendar.DATE);
+        int hour = c.get(Calendar.HOUR_OF_DAY);
+        int minute = c.get(Calendar.MINUTE);
+
+        System.out.println(year + "-" + month + "-" + day + " " + hour + ":" + minute);
+    }
+    @Test
+    public void t9(){
+        Calendar calendar = Calendar.getInstance();
+        Date date = calendar.getTime();
+        date = new Date(234234235235L);
+        calendar.setTime(date);
+        calendar.set(Calendar.DAY_OF_MONTH, 8);
+        System.out.println("当前时间日设置为8后,时间是:" + calendar.getTime());
+        calendar.add(Calendar.HOUR, 2);
+        System.out.println("当前时间加2小时后,时间是:" + calendar.getTime());
+        calendar.add(Calendar.MONTH, -2);
+        System.out.println("当前日期减2个月后,时间是:" + calendar.getTime());
+    }
+
+    @Test
+    public void t10(){
+//        LocalTime localTime = now();
+//        LocalTime localTime1 = localTime.plusHours(10);
+//        System.out.println(localTime1);
+//        LocalDate localDate = LocalDate.from(now());
+//        localDate.getDayOfWeek();
+        LocalDateTime localDateTime = LocalDateTime.now();
+        int dayOfMonth = localDateTime.getDayOfMonth();
+        System.out.println(dayOfMonth);
+
+//        System.out.println(now);
+//        LocalDate localDate = LocalDate.ofYearDay(2002, 366);
+//        LocalDate of = LocalDate.of(2024, 02, 30);
+//        System.out.println(of);
+//        System.out.println(localDate);
+//        LocalTime localTime = new LocalTime(12,12,12,12).plusHours(12);
+
+//        System.out.println(localTime);
+    }
+
+    @Test
+    public void t11() {
+        LocalDate localDate = LocalDate.now();
+        boolean leapYear = localDate.isLeapYear();
+        System.out.println(leapYear);
+//        LocalTime localTime = now();
+        LocalDateTime localDateTime1 = LocalDateTime.now();
+        LocalDateTime localDateTime = LocalDateTime.of(2024, 01, 23, 15, 53);
+        boolean after = localDateTime.isAfter(localDateTime1);
+        System.out.println("===================" + after);
+        boolean before = localDateTime.isBefore(localDateTime1);
+        System.out.println(before);
+
+        System.out.println("......." + localDateTime.getYear());
+
+
+
+        int dayOfMonth = localDateTime.getDayOfMonth();
+        System.out.println(dayOfMonth);
+        int dayOfYear = localDateTime.getDayOfYear();
+        System.out.println(dayOfYear);
+        System.out.println(localDateTime.getDayOfWeek());
+        System.out.println(localDateTime.getMonth());
+        System.out.println(localDateTime.getMonth());
+        System.out.println(localDateTime.getMonthValue());
+        System.out.println(localDateTime.getYear());
+        System.out.println(localDateTime.getMonth());
+        System.out.println(localDateTime.getHour());
+        System.out.println(localDateTime.getMinute());
+        System.out.println(localDateTime.getSecond());
+
+//        LocalDateTime localDateTime1 = localDateTime.withDayOfMonth(12);
+//        System.out.println(localDateTime1);
+//        LocalDateTime localDateTime1 = localDateTime.withYear(2222);
+//        System.out.println(localDateTime1);
+
+//        LocalDateTime localDateTime1 = localDateTime.plusDays(9);
+//        System.out.println(localDateTime1);
+        System.out.println(localDateTime.plusWeeks(2));
+        System.out.println(localDateTime.plusMonths(2));
+        System.out.println(localDateTime.plusHours(2));
+        System.out.println(localDateTime.plusMinutes(2));
+        System.out.println(localDateTime.plusSeconds(60));
+
+
+    }
+}

BIN
target/classes/com/sf/day12/TestApi$1.class


BIN
target/classes/com/sf/day12/TestApi.class