1:递归阶乘
public class Digui {
private static int i=5;
private static int digui(int i){
if(i==1){
return 1;
}
int res = i*digui(i-1);
return res;
}
public static void main(String[] args) {
System.out.println(digui(5));
}
}
2:swing(平台阻碍性win-linux)是awt的升级版 最后集合为swt
3:单例
public class SingleTon { private static SingleTon singleTon=null; private SingleTon() { } public static SingleTon singleTon2(){ if(singleTon==null){ singleTon = new SingleTon(); } return singleTon; } public static void main(String[] args) { SingleTon singleTon1 = singleTon2(); SingleTon singleTon2 = singleTon2(); } }
4:简写枚举
经典枚举public class Enum_main { public static void main(String[] args) { Enum s = Enum.MON; if(s==Enum.TUSE){ System.out.println("星期二"); } } }
public static final int Sun =0b00000001; public static void main(String[] args) { //用一个字符串表示 星期几 int today = Week.MON;//星期三 if(today==Week.MON){ System.out.println("我要逃课去 泡妞!"); }else if(today==Week.THUR){ System.out.println("回学校好好学习!!"); } //..... } }
5:捕获架包和导入架包、执行架包和可执行架包(记住一个概念jar命令和win视图的操作)
6:通过calendar的四种时间设置应用(1:通过getTime()简单的获取时间 2:指定时间获取时间集set(Calendar.YEAR, 1984) 3:指定时间获取获取时间集set(1984, 2, 18, 22, 35, 25) 4:set(1984, 2, 18, 22, 35, 25);)
getTime()是date类的一个方法所以使用时必须调用date对象
public class CalendarDemo { public static void main(String[] args) { //Date 这个类型格式化和反格式化 都很方便,很好用! //Calendar 可以设置具体时间!! // Calendar -----> Date //1:getTime()简单的获取时间 Calendar birthday = Calendar.getInstance(); Date d = birthday.getTime(); System.out.println(d); //2:指定时间获取时间集 /*Calendar birthday = Calendar.getInstance(); birthday.set(Calendar.YEAR, 1984); System.out.println(birthday);*/ //3:指定时间获取获取时间集 /*Calendar birthday = Calendar.getInstance(); birthday.set(1984, 2, 18, 22, 35, 25); Date d = new Date(birthday.getTimeInMillis()); System.out.println(d);*/ //4:通过formatDate2Bar方法格式化后的时间获取 /*Calendar birthday = Calendar.getInstance(); Date d = new Date(birthday.getTimeInMillis()); System.out.println(DateUtil.formatDate2Bar(d));*/ // String str= "1984-03-18 22:35:25";//这种方式少用! } }
时间之间的运算和格式化(1:简单的获取当前时间getTime() 2:时间相减求出去年时间(注意长整形) 3:时间相减求出昨天的时间 4:标准的格式化输出: 5:调用自定义方法输出时间)public class DateDemo { public static void main(String[] args) { //1:简单的获取当前时间getTime() /*Date now = new Date(System.currentTimeMillis()); long time = now.getTime();//时间戳! System.out.println(now);*/ //2:时间相减求出去年时间(注意长整形) /*Date now = new Date(System.currentTimeMillis()); long time = now.getTime(); Date qunian = new Date(time-(365L*24*3600*1000)); System.out.println(qunian);*/ //3:时间相减求出昨天的时间 /*Date date = new Date(System.currentTimeMillis()-(24*3600*1000)); System.out.println(date);*/ //4:标准的格式化输出: /*SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS"); Date date = new Date(System.currentTimeMillis()-(24*3600*1000)); String dateTxt = simpleDateFormat.format(date) ; System.out.println(dateTxt); */ //5:调用自定义方法输出时间------------------------------ /*String s = DateUtil.formatDate2Bar(new Date()); System.out.println(s);*/ } //格林威治时间 }
Date转换为String
public class Person { private Date birthday; private String birthdayTxt; public Person() { } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getBirthdayTxt() { return DateUtil.formatDate2Bar(this.getBirthday()); } }
自定义时间格式方法
public class DateUtil { public static SimpleDateFormat dateFormatBar = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static String formatDate2Bar(Date date){ if(date ==null){ return null; } return dateFormatBar.format(date); } public static String formatDate2Bar(Calendar date){ if(date ==null){ return null; } return dateFormatBar.format(date.getTime()); } public static Date parseDate4Bar(String text){ try { if(text==null){ return null; } return dateFormatBar.parse(text); } catch (ParseException e) { e.printStackTrace(); } return null; } }
时间格式的转换再转换成天数做判断(String---Date)public class MainMain { public static void main(String[] args) { String my = "2000-12-19 11:45:53"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date myDate = simpleDateFormat.parse(my); long day = myDate.getTime()/1000L/3600/24; if(day>30000){ System.out.println("活腻了!"); }else{ System.out.println("烦死了!"); } } catch (ParseException e) { e.printStackTrace(); } } }
Date和calendar 之间的灵活应用public class MainMainMain { public static void main(String[] args) { //获取Date 类型的的昨天此时此刻! Date nowDate = new Date(System.currentTimeMillis() - (24L*3600*1000)); Calendar now = Calendar.getInstance();//当前时间只是一个默认值! //把毫秒数设置给他! now.setTimeInMillis(nowDate.getTime()); System.out.println(now.get(Calendar.DAY_OF_MONTH)); } }