黑马程序员 Date 、Calendar 、数学类和字符编码

来源:互联网 发布:安徽seo 编辑:程序博客网 时间:2024/05/21 17:44

------- android培训、java培训、期待与您交流! ----------


1 Date:

import java.util.*;import java.text.*;class  DateDemo1{public static void main(String[] args) {Date d=new Date();        System.out.println(d);//Fri Dec 21 23:06:30 CST 2012//将模式封装到SimpleDateformat对象中SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日E hh:mm:ss");//调用format方法让模式格式化指定Date对象String time=sdf.format(d);System.out.println("time="+time);//time=2013年3月21日星期四 11:06:30      }}

2 Calendar 

Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEARMONTHDAY_OF_MONTHHOUR日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。

import java.util.*;/*1,获取任意年的二月有多少天。思路:根据指定年设置一个时间就是 c.set(year,2,1)//某一年的3月1日。c.add(Calenar.DAY_OF_MONTH,-1);//3月1日,往前推一天,就是2月最后一天。2,获取昨天的现在这个时刻。c.add(Calenar.DAY_OF_MONTH,-1);*/class  CalendarDemo2{public static void main(String[] args) {Calendar c = Calendar.getInstance();//使用默认时区和语言环境获得一个日历。//c.set(2012,2,23);设置c.add(Calendar.DAY_OF_MONTH,-18);printCalendar(c);}public static void printCalendar(Calendar c){String[] mons = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};String[] weeks = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六",};int index = c.get(Calendar.MONTH);int index1 = c.get(Calendar.DAY_OF_WEEK);sop(c.get(Calendar.YEAR)+"年");//sop((c.get(Calendar.MONTH)+1)+"月");sop(mons[index]);sop(c.get(Calendar.DAY_OF_MONTH)+"日");//sop("星期"+c.get(Calendar.DAY_OF_WEEK));sop(weeks[index1]);}public static void sop(Object obj){System.out.println(obj);}}


3 数学类:

class MathDemo1 {public static void main(String[] args) {for(int x=0;x<10;x++){int d= (int)(Math.random()*10+1);//1到10随机产生10个数字sop(d);}show();}public static void show(){double d = Math.ceil(16.34);//ceil返回大于指定数据的最小整数double d1 = Math.floor(12.34);//floor返回小雨指定数据的最大整数long l = Math.round(12.54);//四舍五入sop("d="+d);sop("d1="+d1);sop("l="+l);double d2=Math.pow(2,3);//2 的3次方sop("d2="+d2);System.out.println("Hello World!");}public static void sop(Object obj){System.out.println(obj);}}



字符编码

字符流的出现为了方便操作字符。
.
更重要是的加入了编码转换。
.
通过子类转换流来完成。
.
InputStreamReader
.
OutputStreamWriter
.
在两个对象进行构造的时候可以加入字符 集。


编码表的由来

计算机只能识别二进制数据,早期由来是 电信号。

为了方便应用计算机,让它可以识别各个 国家的文字。

就将各个国家的文字用数字来表示,并一 一对应,形成一张表。

这就是编码表。


常见的编码表


ASCII:美国标准信息交换码。

用一个字节的7位可以表示。

ISO8859-1:拉丁码表。欧洲码表

用一个字节的8位表示。

GB2312:中国的中文编码表。

GBK:中国的中文编码表升级,融合了更多的中文文字符号。

Unicode:国际标准码,融合了多种文字。

所有文字都用两个字节来表示,Java语言使用的就是unicode

UTF-8:最多用三个字节来表示一个字符。


转换流的编码应用


可以将字符以指定编码格式存储。
.
可以对文本数据指定编码格式来解读。
.
指定编码表的动作由构造函数完成。



字符编码


编码:字符串.字节数组
.
解码:字节数组.字符串


/*编码:字符串变成字节数组。解码:字节数组变成字符串。String-->byte[];  str.getBytes(charsetName);byte[] -->String: new String(byte[],charsetName);*/import java.util.*;class  EncodeDemo{public static void main(String[] args)throws Exception {String s = "哈哈";byte[] b1 = s.getBytes("GBK");//编码:字符串变成字节数组。System.out.println(Arrays.toString(b1));String s1 = new String(b1,"utf-8");System.out.println("s1="+s1);//对s1进行iso8859-1编码。byte[] b2 = s1.getBytes("utf-8");System.out.println(Arrays.toString(b2));String s2 = new String(b2,"gbk");//解码:字节数组变成字符串。System.out.println("s2="+s2);}}

import java.io.*;class EncodeStream {public static void main(String[] args) throws IOException {//writeText();readText();}public static void readText()throws IOException //解码{InputStreamReader isr = new InputStreamReader(new FileInputStream("utf.txt"),"gbk");char[] buf = new char[10];int len = isr.read(buf);String str = new String(buf,0,len);System.out.println(str);isr.close();}public static void writeText()throws IOException //编码{OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("utf.txt"),"UTF-8");osw.write("你好");osw.close();}}







原创粉丝点击