常见的API类
1:String类
(1)概念:字符串:多个字符组成的一串数据。
(2)构造方法:
A:String s = new String();//创建一个String类对象s。
B:String s = new String(byte[] bys);//创建一个String类对象s,并将一个字节数组赋予s。
C:String s = new String(byte[] bys,int index,int length);//创建一个String类对象s,并将一个字节数组的子数组赋予s
D:String s = new String(char[] chs);//创建一个String类对象s,并将一个字符数组赋予s。
E:String s = new String(char[] chs,int index,int length);//创建一个String类对象s,并将一个字符数组的子数组赋予s
F:String s = new String(String str);创建一个String类对象s,并将一个字符串str赋予s。
G:String s = "hello";//创建一个String类对象s,并将字符串hello赋予s。
举例:
成员方法:
public int length():获取字符串的长度
public class StringDemo {
public static void main(String[] args) {
// 方式1 String()
String s1 = new String();
// s1 = "abcde";
System.out.println("s1:" + s1);
System.out.println("s1.length():" + s1.length());
System.out.println("--------------------------");
// 方式2 String(byte[] bytes)
byte[] bys = { 97, 98, 99, 100, 101 };
String s2 = new String(bys);
System.out.println("s2:" + s2);
System.out.println("s2.length()" + s2.length());
System.out.println("--------------------------");
// 方式3 String(byte[] bytes, int index, int length)
String s3 = new String(bys, 2, 3);
// StringIndexOutOfBoundsException 字符串索引越界异常
// String s3 = new String(bys, 2, 32);
System.out.println("s3:" + s3);
System.out.println("s3.length():" + s3.length());
System.out.println("--------------------------");
// 方式4 String(char[] value)
char[] chs = { 'a', 'b', 'c', 'd', 'e' };
String s4 = new String(chs);
System.out.println("s4:" + s4);
System.out.println("s4.length():" + s4.length());
System.out.println("--------------------------");
// 方式5 String(char[] value, int index, int length)
String s5 = new String(chs, 1, 3);
System.out.println("s5:" + s5);
System.out.println("s5.length():" + s5.length());
System.out.println("--------------------------");
// 方式6 String(String str)
String s6 = new String("abcde");
System.out.println("s6:" + s6);
System.out.println("s6.length():" + s6.length());
System.out.println("--------------------------");
// 方式7
String s7 = "abcde";
System.out.println("s7:" + s7);
System.out.println("s7.length():" + s7.length());
System.out.println("--------------------------");
}
}
(3)字符串的特点及面试题
A:字符串一旦被赋值,就不能改变。
注意:字符串的值不能改变,没有说引用变量不能改变。
B:面试题:
a:String s = new String("hello")和String s = "hello"的区别。
b:请写出结果:
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1==s2);//false
System.out.println(s1.equals(s2));//true
String s3 = new String("hello");
String s4 = "hello";
System.out.println(s3==s4);//false
System.out.println(s3.equals(s4));//true
String s5 = "hello";
String s6 = "hello";
System.out.println(s5==s6);//true
System.out.println(s5.equals(s6));//false
注意:“==”比较的是引用类型的地址值
equals基本类型比地址值较的是内容
引用类型比较的是
(4)成员方法(补齐方法意思)
A:判断功能
boolean equals(Object obj)//判断字符串的内容是否相同,区分大小写。
boolean equalsIgnoreCase(String str)//判断字符串的内容是否相同,区分大小写。
boolean contains(String str)//判断字符串对象是否包含给定的字符串。
boolean startsWith(String str)//判断字符串对象是否以给定的字符串开始。
boolean endsWith(String str)//判断字符串对象是否以给定的字符串结尾。
boolean isEmpty()'//判断字符串对象是否为空。数据是否为空。
例子:
public class StringDemo {
public static void main(String[] args) {
// 创建字符串对象
String s = "HelloWorld";
// boolean equals(Object obj):判断字符串的内容是否相同,区分大小写。
System.out.println(s.equals("HelloWorld"));
System.out.println(s.equals("helloworld"));
System.out.println("--------------------");
// boolean equalsIgnoreCase(String str):判断字符串的内容是否相同,不区分大小写。
System.out.println(s.equalsIgnoreCase("HelloWorld"));
System.out.println(s.equalsIgnoreCase("helloworld"));
System.out.println("--------------------");
// boolean contains(String str):判断字符串对象是否包含给定的字符串。
System.out.println(s.contains("or"));
System.out.println(s.contains("ak"));
System.out.println("--------------------");
// boolean startsWith(String str):判断字符串对象是否以给定的字符串开始。
System.out.println(s.startsWith("Hel"));
System.out.println(s.startsWith("hello"));
System.out.println("--------------------");
// boolean endsWith(String str):判断字符串对象是否以给定的字符串结束。省略不讲。
// boolean isEmpty():判断字符串对象是否为空。数据是否为空。
System.out.println(s.isEmpty());
String s2 = "";
System.out.println(s2.isEmpty());
// String s3 = null;
// NullPointerException 空指针异常
// System.out.println(s3.isEmpty());
}
}
B:获取功能
int length()//获取字符串的长度
char charAt(int index)//返回字符串中给定索引处的字符
int indexOf(int ch)//返回指定字符在此字符串中第一次出现的索引
int indexOf(String str)//返回指定字符串在此字符串中第一次出现的索引
int indexOf(int ch,int fromIndex)//返回在此字符串中第一次出现指定字符的索引,从指定的索引开始搜索。
int indexOf(String str,int fromIndex)//返回在此字符串中第一次出现指定字符串的索引,从指定的索引开始搜索。
String substring(int start)//截取字符返回从指定位置开始截取后的字符串。
String substring(int start,int end)//截取字符串。返回从指定位置开始到指定位置结束截取后的字符串。
例子:
public class StringDemo {
public static void main(String[] args) {
// 创建字符串对象
String s = "helloworld";
// int length():获取字符串的长度
System.out.println(s.length());
System.out.println("--------");
// char charAt(int index):返回字符串中给定索引处的字符
System.out.println(s.charAt(2));
System.out.println("--------");
// 遍历字符串。
for (int x = 0; x < s.length(); x++) {
char ch = s.charAt(x);
System.out.println(ch);
}
System.out.println("--------");
// int indexOf(int ch):返回指定字符在此字符串中第一次出现的索引
System.out.println(s.indexOf('l'));
// int indexOf(int ch,int fromIndex):返回在此字符串中第一次出现指定字符的索引,从指定的索引开始搜索。
System.out.println(s.indexOf('l', 4));
System.out.println("--------");
// 常见的方法:包左不包右。
// String substring(int start):截取字符串。返回从指定位置开始截取后的字符串。
System.out.println(s.substring(4));
// String substring(int start,int end)截取字符串。返回从指定位置开始到指定位置结束截取后的字符串。
System.out.println(s.substring(4, 8));
//截取的串要和以前一样。
System.out.println(s.substring(0));
System.out.println(s.substring(0,s.length()));
System.out.println(s);
}
}
C:转换功能byte[] getBytes()//把字符串转换成字节数组。
char[] toCharArray()//把字符串转换成字符数组。
static String copyValueOf(char[] chs)//把字符数组转换成字符串。
static String valueOf(char[] chs)://把字符数组转换成字符串。
static String valueOf(int i)//基本类型:把int(基本类型)转换成字符串。
String toLowerCase()//把字符串变成小写
String toUpperCase()://把字符串变成大写
String concat(String str)//拼接字符串。
例子:
public class StringDemo {
public static void main(String[] args) {
// 创建字符串对象
String s = "HelloWorld";
// byte[] getBytes():把字符串转换成字节数组。
byte[] bys = s.getBytes();
for (int x = 0; x < bys.length; x++) {
System.out.println(bys[x]);
}
System.out.println("-----------------");
// char[] toCharArray():把字符串转换成字符数组。
char[] chs = s.toCharArray();
for (int x = 0; x < chs.length; x++) {
System.out.println(chs[x]);
}
System.out.println("-----------------");
// static String copyValueOf(char[] chs):把字符数组转换成字符串。
char[] chs2 = { 'a', 'b', 'c', '中', '国' };
String s2 = String.copyValueOf(chs2);
System.out.println(s2);
System.out.println("-----------------");
// static String valueOf(char[] chs):把字符数组转换成字符串。
String s3 = String.valueOf(chs2);
System.out.println(s3);
System.out.println("-----------------");
// static String valueOf(int i)
int i = 100;
String s4 = String.valueOf(i);
System.out.println(s4);
System.out.println("-----------------");
// String toLowerCase():把字符串变成小写
System.out.println(s.toLowerCase());
// String toUpperCase():把字符串变成大写
System.out.println(s.toUpperCase());
System.out.println("-----------------");
// String concat(String str):拼接字符串。
String s5 = "hello";
String s6 = s5 + "world";
String s7 = s5.concat("world");
System.out.println(s6);
System.out.println(s7);
}
}
D:其他功能a:替换功能
String replace(char oldChar,char newChar)//用新的字符去替换指定的旧字符
String replace(String oldString,String newString)//用新的字符串去替换指定的旧字符串
b:切割功能
String[] split(String regex)
c:去除两端空格功能
String trim()
d:字典顺序比较功能
int compareTo(String str)
int compareToIgnoreCase(String str)
public class StringDemo {
public static void main(String[] args) {
// 替换功能
String s = "helloworld";
System.out.println(s.replace('l', 'p'));
System.out.println(s.replace("ll", "ak47"));
// 切割功能
String ages = "20-30";
// if(age>=20 && age<=30){人显示出来供我选择}
String[] strArray = ages.split("-");
for (int x = 0; x < strArray.length; x++) {
// String -- int
System.out.println(strArray[x]);
}
// 去除两端空格功能
String name = " admin hello ";
System.out.println("***" + name.trim() + "***");
// 按字典顺序比较两个字符串
String s1 = "hello";
String s2 = "aello";
String s3 = "mello";
String s4 = "hello";
String s5 = "Hello";
//System.out.println('m'+1);//109
System.out.println(s1.compareTo(s2));// 7
System.out.println(s1.compareTo(s3));// -5
System.out.println(s1.compareTo(s4));// 0
System.out.println(s1.compareTo(s5));//32
}
}
(5)案例:A:字符串遍历
public class StringDemo {
public static void main(String[] args) {
// 创建字符串对象
String s = "helloworld";
// 遍历字符串。
for (int x = 0; x < s.length(); x++) {
char ch = s.charAt(x);
System.out.println(ch);
}
System.out.println("--------");
}
B:统计字符串中大写,小写,数字字符出现的次数/*
* 统计大写小写字符的个数
*
* 举例:
* Hello12345World
*
* 大写:2
* 小写:8
* 数字:5
*
* 思路:
* A:定义三个统计变量
* B:获取到每一个字符。遍历字符串。
* C:判断是属于哪种范围的
* 大写:65-90
* 小写:97-122
* 数字:48-57
* C:是哪种哪种++
*/
public class StringTest {
public static void main(String[] args) {
String s = "Hello12345World";
// 定义三个统计变量
int bigCount = 0;
int smallCount = 0;
int numberCount = 0;
// 遍历字符串。
for (int x = 0; x < s.length(); x++) {
char ch = s.charAt(x);
//方式1
// 判断是属于哪种范围的
// if (ch >= 65 && ch <= 90) {
// bigCount++;
// } else if (ch >= 97 && ch <= 122) {
// smallCount++;
// } else if (ch >= 48 && ch <= 57) {
// numberCount++;
// }
//方式2
if (ch >= 'A' && ch <= 'Z') {
bigCount++;
} else if (ch >= 'a' && ch <= 'z') {
smallCount++;
} else if (ch >= '0' && ch <= '9') {
numberCount++;
}
}
System.out.println("大写:" + bigCount);
System.out.println("小写:" + smallCount);
System.out.println("数字:" + numberCount);
}
}
C:把字符串的首字母大写,其他小写
/*
* 需求:把字符串的首字母转成大写,其余为小写
*
* 举例:
* helloWorld
*
* 结果:
* Helloworld
*
* 思路:
* A:截取首字母。
* B:截取其他字母。
* C:把A转大写+B转小写
*/
public class StringTest {
public static void main(String[] args) {
String s = "helloWorld";
String s1 = s.substring(0, 1);
String s2 = s.substring(1);
String s3 = s1.toUpperCase().concat(s2.toLowerCase());
System.out.println(s3);
// 链式编程
// String result = s.substring(0, 1).toUpperCase()
// .concat(s.substring(1).toLowerCase());
// System.out.println(result);
}
}
D:把字符串中字符排序
数组排序(冒泡排序 -- 相邻元素两两依次比较,大的往后放。)
/*
* 对字符串中字符进行自然排序:
* "basckd" -- "abcdks"
*
* 思路:
* A:把字符串变成字符数组
* B:对字符数组进行排序
* C:把排序后的字符数组转换成字符串
*/
public class StringTest3 {
public static void main(String[] args) {
String s = "basckd";
// 把字符串变成字符数组
char[] chs = s.toCharArray();
// 对字符数组进行排序
bubbleSort(chs);
// 把排序后的字符数组转换成字符串
/*
* 方式1:构造方法
* 方式2:静态方法 copyValueOf()
* 方式3:静态方法 valueOf()
*/
String result = new String(chs);
System.out.println(result);
}
public static void bubbleSort(char[] chs) {
for (int x = 0; x < chs.length - 1; x++) {
for (int y = 0; y < chs.length - 1 - x; y++) {
if (chs[y] > chs[y + 1]) {
char ch = chs[y];
chs[y] = chs[y + 1];
chs[y + 1] = ch;
}
}
}
}
}
E:编写程序,从键盘接收一个字符串,对字符串中的字母进行大小写互转(大写字母转成小写,小写字母转成大写)。
public class Test8 {
public static void main(String[] args) {
// 创建键盘输入Scanner对象
Scanner sc = new Scanner(System.in);
// 输出提示
System.out.println("请输入一个字符串(由A-Z和a-z组成):");
// 定义String类型的str变量接收
String str = sc.nextLine();
// 将输入的字符串转化为char类型数组
char[] ch = str.toCharArray();
System.out.println("大小写互转后:");
for (int i = 0; i < ch.length; i++) {
// 判断,如果在A和Z之间,则转换为小写
if (ch[i] >= 'A' && ch[i] <= 'Z') {
ch[i] = Character.toLowerCase(ch[i]);
System.out.print(ch[i]);
}
// 判断,如果在a和z之间,则转换为大写
else if (ch[i] >= 'a' && ch[i] <= 'z') {
ch[i] = Character.toUpperCase(ch[i]);
System.out.print(ch[i]);
}
}
}
}
2:Arrays工具类的使用(掌握)
(1)Arrays是针对数组操作的工具类。
(2)成员方法:
public static String toString(数组):把数组变成字符串。
public static void sort(数组):对数组进行排序。
public static int binarySearch(int[] arr,int value):二分查找
import java.util.Arrays;
public class ArraysDemo {
public static void main(String[] args) {
// 定义数组
int[] arr = { 23, 84, 51, 72, 69 };
// public static String toString(int[] a):把整型数组转变成字符串。
String str = Arrays.toString(arr);
System.out.println(str);
// public static void sort(int[] a):对数组进行排序
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
// public static int binarySearch(int[] a,int key):对数组进行二分查找。
int[] arr2 = { 12, 23, 34, 45, 56, 67 };
// 查找23的索引
System.out.println(Arrays.binarySearch(arr2, 23));
// 查找27的索引
System.out.println(Arrays.binarySearch(arr2, 27));
}
}
3:System的方法(掌握)
(1)系统类,提供了静态的变量和方法供我们使用。
(2)成员方法:
public static void exit(int value):退出jvm,非0表示异常退出。
public static long currentTimeMillis():返回当前系统时间的毫秒值。
和1970 年 1 月 1 日午夜之间的时间差
public class SystemDemo {
public static void main(String[] args) {
// public static void exit(int status)
// System.exit(100);
System.out.println("hello");
// public static long currentTimeMillis():返回以毫秒为单位的当前时间。
long time = System.currentTimeMillis();
System.out.println(time);
// 需求,请测试下面代码的运行时间
// long start = System.currentTimeMillis();
// for (int x = 0; x < 1000000; x++) {
// System.out.println(x);
// }
// long end = System.currentTimeMillis();
// System.out.println((end - start) + "毫秒");
// public static void arraycopy(Object src,int srcPos,Object dest,int
// destPos,int length)
int[] arr = { 1, 2, 3, 4, 5 };
int[] arr2 = { 5, 6, 7, 8, 9 };
System.arraycopy(arr, 3, arr2, 3, 2);
// System.arraycopy(arr, 4, arr2, 3, 2);
System.out.println(Arrays.toString(arr));
System.out.println(Arrays.toString(arr2));
}
}
4:StringBuffer
(1)字符个数可以发生改变的字符串类。字符串缓冲区类。
(2)构造方法:
A:StringBuffer()
B:StringBuffer(int capacity)
C:StringBuffer(String str)
public class StringBufferDemo {
public static void main(String[] args) {
// 方式1
StringBuffer sb = new StringBuffer();
System.out.println("sb:" + sb);
System.out.println("sb.length():" + sb.length());
System.out.println("sb.capacity():" + sb.capacity());
System.out.println("------------------------------");
// 方式2
StringBuffer sb2 = new StringBuffer(50);
System.out.println("sb2:" + sb2);
System.out.println("sb2.length():" + sb2.length());
System.out.println("sb2.capacity():" + sb2.capacity());
System.out.println("------------------------------");
// 方式3
StringBuffer sb3 = new StringBuffer("hello");
System.out.println("sb3:" + sb3);
System.out.println("sb3.length():" + sb3.length());
System.out.println("sb3.capacity():" + sb3.capacity());
}
}
(3)成员方法:(自己补齐)A:添加功能
public StringBuffer append(int i):在末尾追加元素
public StringBuffer insert(int index,int i):在指定位置添加元素
例子:
public class StringBufferDemo2 {
public static void main(String[] args) {
// 创建对象
StringBuffer sb = new StringBuffer();
// StringBuffer sb2 = sb.append(100);
// System.out.println(sb);
// System.out.println(sb2);
// System.out.println(sb == sb2);// true
// sb.append(100);
// sb.append("hello");
// sb.append(true);
// sb.append(12.5);
// 链式变成
sb.append(100).append("hello").append(true).append(12.5);
//100hellotrue12.5
sb.insert(8, "world");
System.out.println("sb:" + sb);
//sb = "helloworld";
}
}
B:删除功能
StringBuffer deleteCharAt(int index):删除指定位置字符
StringBuffer delete(int start, int end):删除指定开始位置和结束位置间的字符
例子:
public class StringBufferDemo3 {
public static void main(String[] args) {
// 定义StringBuffer
StringBuffer sb = new StringBuffer();
sb.append("hello").append("world").append("java");
// StringBuffer deleteCharAt(int index)
// sb.deleteCharAt(1);
// StringBuffer delete(int start, int end)
// sb.delete(5, 10);
// 我要删除所有元素
sb.delete(0, sb.length());
System.out.println(sb);
}
}
C:替换功能StringBuffer replace(int start, int end, String str):把开始到结束位置的字符用一个新的字符串给替换。
例子:
public class StringBufferDemo4 {
public static void main(String[] args) {
// 定义StringBuffer
StringBuffer sb = new StringBuffer();
sb.append("hello").append("world").append("java");
sb.replace(5, 10, "刘亦菲");
System.out.println(sb);
}
}
D:截取功能String substring(int start):从指定位置到末尾截取
String substring(int start, int end): 从指定位置到结束位置截取
Example:
public class StringBufferDemo5 {
public static void main(String[] args) {
// 定义对象
StringBuffer sb = new StringBuffer();
// 添加元素
sb.append("hello").append("world").append("java");
// 截取
String s = sb.substring(5);
// 输出
System.out.println(s);
System.out.println(sb);
}
}
E:反转功能StringBuffer reverse():字符串反转
Example:
public class StringBufferDemo6 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append("hello").append("world");
// 反转
sb.reverse();
System.out.println(sb);
}
}
F:案例字符串反转
import java.util.Scanner;
/*
* 需求:键盘录入一个字符串,请把字符串的数据反转后输出。
*
* 举例:
* 输入:abc
* 结果:cba
*
* 方式1:
* A:把字符串变成字符数组
* B:数组倒着打
* 方式2:
* 想使用StringBuffer的reverse()方法。
* A:String -- StringBuffer 构造方法
* B:StringBuffer -- reverse()
* C:StringBuffer -- String 构造方法,toString()
*/
public class StringBufferTest {
public static void main(String[] args) {
// 方式1
// Scanner sc = new Scanner(System.in);
// System.out.println("请输入字符串:");
// String line = sc.nextLine();
// char[] chs = line.toCharArray();
// for (int x = chs.length - 1; x >= 0; x--) {
// System.out.print(chs[x]);
// }
// 方式2
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串:");
String line = sc.nextLine();
StringBuffer sb = new StringBuffer(line);
// 下面也可以
// StringBuffer sb = new StringBuffer();
// sb.append(line);
sb.reverse();
String result = new String(sb);
// 下面这个也可以
// String result = sb.toString();
System.out.println(result);
// 链式编程
// String result2 = new StringBuffer(line).reverse().toString();
// System.out.println(result2);
}
}
5:基本类型包装类(掌握)
(1)基本类型的数据我们只能使用值,不能做更多的操作。为了方便我们操作,
java就把每种基本类型进行了包装。提供方法供我们使用。
(2)基本类型和包装类的对应关系
byte
short
int Integer
long
float
double
char Character
boolean
(3)Integer
构造方法
A:Integer i = new Integer(int num);
B:Integer i = new Integer(String s);
关于Integer的面试题:
byte常量池。
也就是byte范围内的值,直接赋值给Integer,是从常量池里面获取的。
注意:s必须是一个由数字字符组成的字符串。
public class IntegerTest {
public static void main(String[] args) {
Integer i1 = new Integer(127);
Integer i2 = new Integer(127);
System.out.println(i1 == i2);// false
System.out.println(i1.equals(i2));// true
Integer i3 = new Integer(128);
Integer i4 = new Integer(128);
System.out.println(i3 == i4);// false
System.out.println(i3.equals(i4));// true
Integer i5 = 128;
Integer i6 = 128;
System.out.println(i5 == i6);// false
System.out.println(i5.equals(i6));// true
Integer i7 = 127;
Integer i8 = 127;
System.out.println(i7 == i8);// true
System.out.println(i7.equals(i8));// true
}
}
(4)String和int类型的转换
A:String -- int
Integer:
public static int parseInt(String s)
B:int -- String
Integer:
public static String toString(int i)
String:
public static String valueOf(int i)
(5)JDK5以后的新特性
A:自动装箱 基本类型--引用类型
B:自动拆箱 引用类型--基本类型
举例:
Integer i = 100;
i += 200;
(6)面试题:byte常量池
(7)案例:
把字符串中的数字字符排序。
需求:"23 98 71 54 60"
结果:"23 54 60 71 98"
思路:
A:字符串 -- 字符串数组
B:字符串数组 -- int数组
C:int[]排序
D:把排序后的int[] -- String
public class StringTest {
public static void main(String[] args) {
String s = "23 98 71 54 60";
// 字符串 -- 字符串数组
String[] strArray = s.split(" ");
// 字符串数组 -- int数组
int[] arr = new int[strArray.length];
// 循环遍历赋值
for (int x = 0; x < arr.length; x++) {
arr[x] = Integer.parseInt(strArray[x]);
}
//int[]排序
Arrays.sort(arr);
//把排序后的int[] -- String
StringBuffer sb = new StringBuffer();
for(int x=0; x<arr.length ;x++){
sb.append(arr[x]).append(" ");
}
String result = sb.toString().trim();
System.out.println(result);
}
}
6:Date:类 Date 表示特定的瞬间,精确到毫秒。
构造方法:
Date():默认指当前系统时间。
Date(long time):根据给定的毫秒值生成一个时间。
成员方法:
public long getTime():
public void setTime(long time):
Date -- long 通过日期获取毫秒值
Date d = new Date();
long time = d.getTime();
long -- Date 通过毫秒值得到日期对象
long time = ???;
Date d = new Date(time);
Date d2 = new Date();
d2.setTime(time);
Example1:
public class DateDemo {
public static void main(String[] args) {
// 方式一
Date d = new Date();
// Sat Dec 21 15:40:24 CST 2013
System.out.println(d);
System.out.println("****************");
//方式二
//1387611761750 刚才的毫秒值
//Sat Dec 21 15:42:41 CST 2013
// long time = System.currentTimeMillis();
System.out.println(time);
// Date d2 = new Date(time);
Date d2 = new Date(1387611761750L);
System.out.println(d2);
System.out.println("-------------------");
Date d3 = new Date();
d3.setTime(1387612000390L);
// 1387612000390
// Sat Dec 21 15:46:40 CST 2013
//System.out.println(d3.getTime());
System.out.println(d3);
}
}
Example2:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/*
* DateFormat:对日期进行格式化的类。提供了对日期进行格式化,和对字符串进行解析的功能。
*
* Date -- String
* public final String format(Date date)
* 需要自己指定格式,常见的格式:
* yyyy年MM月dd日 HH:mm:ss
yyyy年MM月dd日
HH:mm:ss
yyyy-MM-dd HH:mm:ss
* String -- Date
* public Date parse(String source)
* 注意:如果是字符串到日期,你指定的格式必须和字符串的格式匹配。
*
* 2013-12-12
* yyyy-MM-dd
*
* 2013/11/11
* yyyy/MM/dd
*/
public class DateFormatDemo {
public static void main(String[] args) throws ParseException {
//从Date--String
// 创建日期对象
Date d = new Date();
// Sat Dec 21 16:16:40 CST 2013
// System.out.println(d);
// 创建格式对象
// DateFormat df = new SimpleDateFormat();//多态
// SimpleDateFormat sdf = new SimpleDateFormat();// 用默认的模式
// 默认模式不是我们想要的,所以,我们要指定模式
// 怎么指定模式,获取说,这个模式是什么样子的?
//2013年12月21日 16:23:34
//yyyy年MM月dd日 HH:mm:ss
//SimpleDateFormat(String pattern)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String str = sdf.format(d);
System.out.println(str);
System.out.println("************");
//从String--Date
String s = "2013-12-12 23:12:34";
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dd = sdf2.parse(s);
System.out.println(dd);
}
}
7:Calendar(理解)
(1)Calendar是日历类,它可以获取任意指定日历值,然后自由组合。
(2)成员方法:
get(日历字段):根据给定的日历字段获取值
set(年,月,日):给日历设定指定的年,月,日
add(日历字段,值):给指定的日历字段添加或者减去给定的值。取决于值的正负。
import java.util.Calendar;
/*
* Calendar:日历类。也是处理时间的。
*
* 在日历字段和Calendar之间提供了转换功能:
* 从Calendar获取到任意一个日历字段。然后,按照我需要的数据进行组合。
*
*
* 成员方法:
* public int get(int field):参数是日历字段。
*/
Example:
public class CalendarDemo {
public static void main(String[] args) {
// public static Calendar getInstance()
Calendar c = Calendar.getInstance();// 多态
// public static final int YEAR 年的字段
// public int get(int field):参数是日历字段。
int year = c.get(Calendar.YEAR);
// System.out.println(year);
// 月份
int month = c.get(Calendar.MONTH);
// 月份 0 - 11
// System.out.println(month + 1);
// 日
int date = c.get(Calendar.DATE);
// System.out.println(date);
// 时
int hour = c.get(Calendar.HOUR_OF_DAY);
// System.out.println(hour);
// 分
int minute = c.get(Calendar.MINUTE);
// System.out.println(minute);
// 秒
int second = c.get(Calendar.SECOND);
// System.out.println(second);
// 自己拼接
String s = year + "年" + (month + 1) + "月" + date + "日" + " " + hour
+ ":" + minute + ":" + ((second>9)?second:"0"+second);
System.out.println(s);
}
}
Example2:
package cn.itcast_02;
import java.util.Calendar;
import java.util.Scanner;
/*
* 请说出任意一年的2月份是多少天。
*/
public class CalendarDemo2 {
public static void main(String[] args) {
// 获取Calendar对象
Calendar c = Calendar.getInstance();
// public final void set(int year,int month,int date)
// 注意,这里给的2其实是3月份。
// c.set(2013, 2, 3);
// 2013,3,3
// public abstract void add(int field,
// int amount)根据日历的规则,为给定的日历字段添加或减去指定的时间量。
// c.add(Calendar.MONTH,2);
// c.add(Calendar.DATE, -20);
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
c.set(year, 2, 1);// 把日期设置为2013年3月1日
c.add(Calendar.DATE, -1);// 把日期往前推1日
// System.out.println(c.get(Calendar.YEAR));
// System.out.println(c.get(Calendar.MONTH) + 1);
System.out.println(c.get(Calendar.DATE));
}
}