实训java第十二课 时间 日期 Date Calendar DateFormat StringBuffer StringBuilder String正则表达式 包装类时间:2022-08-25 18:47:48package com.tarena.day12;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.Scanner;public class EXPDemo {public static void main(String[] args) throws ParseException {Scanner sc = new Scanner(System.in);String str = sc.nextLine();DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");Date date = fmt.parse(str);date = specDate(date, 3);//返回促销时间System.out.println(fmt.format(date));}private static Date specDate(Date date, int i) {Calendar cal =new GregorianCalendar();cal.setTime(date);cal.add(Calendar.MONTH, 3);cal.add(Calendar.WEEK_OF_MONTH, -2);cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);cal.get(Calendar.SUNDAY);return cal.getTime();}}package com.tarena.day12;public class StringBufferTest {/** * @param args */public static void main(String[] args) {int time = 50000;System.out.println(testStringBuilder(10000000));//System.out.println(testString(time));}public static long testString(int times){long start = System.currentTimeMillis();String s = "";for (int i = 0; i < times; i++) {s = s + "a";}long end = System.currentTimeMillis();return end - start;}public static long testStringBuilder(int times){long start = System.currentTimeMillis();StringBuilder s = new StringBuilder();//StringBuffer s = new StringBuffer();for (int i = 0; i < times; i++) {s = s.append("a") ;}long end = System.currentTimeMillis();return end - start;}}package com.tarena.day12;import java.util.Arrays;public class StringBuilderDemo {/** * @param args */public static void main(String[] args) {char[] chs ={'北','京','达','内','陪','训','学','校'};System.out.println(chs);//删除“培训”chs[4] = chs[6];chs[5] = chs[7];chs = Arrays.copyOf(chs, chs.length -2);System.out.println(chs);StringBuilder buf = new StringBuilder("北京达内培训学校");buf.delete(4, 6);System.out.println(buf);StringBuilder sbl = new StringBuilder("李敖");sbl.append("的").append("前妻是").append("胡茵梦").insert(0, "大师").delete(4, 5);System.out.println(sbl);}}package com.tarena.day12;import java.util.Scanner;public class StringRegxDemo {public static void main(String[] args) {Scanner console = new Scanner(System.in);String date ;while(true){date = console.nextLine();String regx = "^//d{4}年(//d{1,2}[月日]){2}";if(date.matches(regx)){System.out.println("日期正确!"+date);break;}}String[] data =date.split("[年月日]");int year = Integer.parseInt(data[0]);System.out.println(2011-year);String str = date.replaceAll("[年月日]", "/");System.out.println(str);}}package com.tarena.day12;public class WorpClassDemo {public static void main(String[] args) {int i = 1;Integer one = new Integer(i);Object o = one;int b = one.intValue() +2;//包装类就是基本数据的对象类型//这样Object就可以引用任何数据类型了//补丁:自动包装,自动拆包Integer x = 1; //x= new Integer(1);int y = x+2; //y = x.intValue()+2Object obj = 3;if(obj instanceof Integer){int z= (Integer)obj +2;System.out.println(z);}String hex = "ff";int c = Integer.parseInt(hex,16);System.out.println(c);String str = Integer.toHexString('中');System.out.println(str);str = Integer.toBinaryString('中');System.out.println(str);Float f;}}package com.tarena.day12;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;public class DateTimeDemo {/** * @param args */public static void main(String[] args) {//char[] chs + 操作; StringBuilder//long time + 操作 DateDate date = new Date(0);System.out.println(date);System.out.println(date.getMonth());System.out.println(date.getYear());date.setTime(System.currentTimeMillis());long max = 0x7fffffffffffffffL;System.out.println(max/1000/60/60/24/365);Calendar cal = new GregorianCalendar();cal.setTime(date);int year = cal.get(Calendar.YEAR);System.out.println(year);int day = cal.get(Calendar.DAY_OF_YEAR);System.out.println(day);int mouth = cal.get(Calendar.MONTH);System.out.println(mouth);DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String s = fmt.format(cal.getTime());System.out.println(s);}}