第十一次:常用类
1.常用类:java.util.Date日期类 java.util.Locale 地区类 java.util.Calendar 日历类
javax.text.DateFormat 日期格式类
11.1 Date类
1. 调用包:import java.util.*;
2. Date d=new Date();
int yy=d.getYear()+1900;
int mm=d.getMonth()+1;
int dd=d.getDate();
int hh=d.getHours();
int min=d.getMinutes();
int ss=d.getSeconds();
System.out.println(yy);
System.out.println(mm);
System.out.println(dd);
System.out.println(hh);
System.out.println(min);
System.out.println(ss);
System.out.println(d.toString());
System.out.println(d.toLocaleString());//按当前区域的格式输出
System.out.println(d.getTime());//将时间精确到毫秒
3.//计算程序的运行时间
Date d1=new Date();
System.out.println(d1.getTime());
System.out.println(d1.getTime()-d.getTime());
4.把d的时间传给d2 Date d2=newDate(d.getTime());
5.截获日期和时间
String time=d.toLocaleString();
System.out.println(time.substring(0,time.indexOf("-")));
System.out.println(time.substring(time.indexOf("-")+1,time.lastIndexOf("-")));
System.out.println(time.substring(time.lastIndexOf("-")+1,time.indexOf("")));
System.out.println(time.substring(time.indexOf(" ")+1,time.indexOf(":")));
System.out.println(time.substring(time.indexOf(":")+1,time.lastIndexOf(":")));
System.out.println(time.substring(time.lastIndexOf(":")+1,time.length()));
11.2 Locale 区域类
1.引包 importjava.util.Locale;
2.以某种文字的形式默认显示(日语) Locale.setDefault(Locale.JAPAN);
3. Locale[] l=new Locale[]{Locale.PRC,Locale.TAIWAN,Locale.FRANCE,
Locale.KOREA};
for(Locale locale:l){
System.out.print(locale.getDisplayCountry()+" ");
System.out.println(locale.getDisplayLanguage());
}
11.3 Calender 日历类
1.日历类不允许new ,只允许得
2.根据当前区域设置日历 Calendar c=Calendar.getInstance();
3.常用的方法:c.get(int) c.set(int,int) c.add(int,int) Date c.getTime
4.Calendar自动进位 YEAR和DATE不用加1,其他的在计算的时候要加1。
5.例:2011年8月17日17时55分13秒,星期,这个月的第几个星期,这个月有多少天,距离过年还有多少天
c.set(2011, 7, 13, 17, 55,13);
System.out.print(c.get(Calendar.YEAR)+"年");
System.out.print(c.get(Calendar.MONTH)+1+"月");
System.out.print(c.get(Calendar.DATE)+"号");
System.out.print(c.get(Calendar.HOUR)+"点");
System.out.print(c.get(Calendar.MINUTE)+"分");
System.out.print(c.get(Calendar.SECOND)+"秒");
System.out.println("星期"+(c.get(Calendar.DAY_OF_WEEK)-1));
System.out.println("这个月的第"+c.get(Calendar.WEEK_OF_MONTH)+"个星期");
System.out.println("这个月有"+c.getActualMaximum(Calendar.DATE)+"天");
6. java.nio.charset.Charset; java里可以存放多少字集
Map
for(java.util.Map.Entry
System.out.println(en.getKey() +"\t" + en.getValue());
}
11.4 SimpleDateFormat 时间格式转化
1.导包 java.text.simpleDateFormat
String time = "2014/04/0522:23:24";
SimpleDateFormat s1 = newSimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date d = s1.parse(time);//文本型转换成日期对象
System.out.println(d.toLocaleString());
SimpleDateFormat s1 = newSimpleDateFormat("yy年MM月dd号 HH时mm分ss秒");
String time = s1.format(new Date()); 日期型转换成自己想要的格式
Locale.setDefault(Locale.JAPAN);
11.5 DateFormat
Locale[] l = new Locale[] { Locale.KOREA,Locale.JAPAN, Locale.CHINA,
Locale.TAIWAN, Locale.UK, Locale.US,Locale.CANADA,
Locale.FRANCE };
int[] style = new int[] { DateFormat.FULL,DateFormat.LONG,
DateFormat.MEDIUM, DateFormat.SHORT };
Date time = new Date();
for (Locale locale : l) {
System.out.println(locale.getDisplayCountry()+ " 语言:"
+locale.getDisplayLanguage());
for (int i : style) {
DateFormat date =DateFormat.getDateInstance(i, locale);
System.out.println("\t" +date.format(time));
}
}
getDateInstance(i,locale)获取日期格式器,该格式器具有给定语言环境的给定格式化风格
11.6 集合
1.集合是无限存储容器,比数组大得多,每次存放都会申请一次新内存,申请更大的内存,将小内存释放掉。
2.方法一:易于理解,但运行速度慢
public class Vector {
private Object[] objs = null;
public int size() {
return objs.length;
}
public void add(Object obj) { //添加
if (objs == null) {
objs = new Object[1];
objs[0] = obj;
} else {
Object[] objs1 = new Object[objs.length + 1];
for (int i = 0; i < objs.length; i++) {
objs1[i] = objs[i];
}
objs1[objs.length] = obj;
objs = objs1;
}
}
public void remove(int index){ //删除
Object[] objs1 = new Object[objs.length - 1];
int y=0;
for (int i = 0; i < objs.length; i++) {
if(i!=index){
objs1[y++]=objs[i];
}
}
objs = objs1;
}
public Object get(int index) {
return objs[index];
}
}
方法二:运行速度较快,内存占有率相对较小
private int count=0;
public void add(Object obj) {
if (objs == null) {
objs = new Object[6];
objs[0] = obj;
count++;
} else {
if(count>=objs.length){
Object[] objs1 = new Object[(int)(objs.length *1.5F)];
for (int i = 0; i < objs.length; i++) {
objs1[i] = objs[i];
}
objs = objs1;
}
objs[count] = obj;
count++;
}
}
方法三:是由本地机器语言编写,运算速度很快,但内存占有率过大
private int count=0;
public void add(Object obj) {
if (objs == null) {
objs = new Object[6];
objs[0] = obj;
count++;
} else {
if(count>=objs.length){
Object[] objs1 = new Object[(int)(objs.length *1.5F)];
System.arraycopy(objs, 0, objs1, 0, objs.length);
objs = objs1;
}
objs[count] = obj;
count++;
}
}