1,SimpleDateFormat的使用
SimpleDateFormat的使用,SimpleDateFormat对日期Date类的格式化和解析
1,两个操作:
1.1:格式化:日期--->字符串
1.2:解析:格式化的逆过程,字符串--->日期
2,SompleDateFormat的实例化
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Main {
public void testSimpleDateFormat() throws ParseException {
//实例化SimpleFateFormat
SimpleDateFormat sdf =new SimpleDateFormat();
//格式化:日期--->字符串
Date date=new Date();
System.out.println(date);
String format=sdf.format(date);
System.out.println(format);
//解析:格式化的逆过程,字符串--->日期
String str="2024-04-19 上午11:43";
Date date1=sdf.parse(str);
System.out.println(date1);
//按照指定的方式格式化
SimpleDateFormat sdf1=new SimpleDateFormat("yyyyy.MMMMM.dd GGG hh:mm aaa");
//格式化
String format1=sdf1.format(date);
System.out.println(format1);
//解析:要求字符串必须是符合SimpleDateFormat识别的格式(通过构造器参数体现),否则就会抛异常
Date date2=sdf1.parse("2020-02-18 11:48:27");
System.out.println(date2);
}
练习一:字符串“2020-09-08“转换为java.slq.Date
public static void main(String[] args) throws ParseException {
String birth="2020-09-08";
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");
Date date=sdf1.parse(birth);
System.out.println(date);
}
2,Calendar日历类的使用
1,实例化
方式一:创建其子类(GregorianCalendar)的对象
方式二:调用其静态方法getInstance()
2,常用方法
get()
set()
add()
getTime():日历类-->Date
setTime():Date-->日历类
以下是关于实例化和常用方法的格式
public void testCalendar(){
//1,实例化
//方式一:创建其子类(GregorianCalendar)的对象
//方式二:调用其静态方法getInstance()
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getClass());
//2,常用方法
//get()
int days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
//set()
calendar.set(Calendar.DAY_OF_MONTH,22);
days=calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
//add()
calendar.add(Calendar.DAY_OF_MONTH,3);
days=calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
//getTime():日历类--->Date
Date date = calendar.getTime();
System.out.println(date);
//setTime():Date--->日历类
Date date1 = new Date();
calendar.setTime(date1);
}
3,Comparator接口的使用
Comparator接口的使用:定制排序
1,背景: 当元素的类型没有实现java.lang.Comparable接口而又不方便修改代码 或者实现类
java.lang.Comparable接口的排序规则不适合当前的操作 那么可以考虑使用Comparator的对象来
排序
2,重写compare(Object o1,Object o2)方法,比较o1和o2的大小:如果方法返回正整数,则表示
o1大于o2,如果为负数,则表示o1小于o2,如果返回0,则表示o1等于o2
以下是关于Comparator接口的使用的例子
@Test
public void test2(){
String[] arr=new String[]{"AA","CC","MM","GG","JJ","DD","KK"};
Arrays.sort(arr,new Comparator(){
//按照字符串从大到小的顺序排列
@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof String&&o2 instanceof String){
String s1=(String) o1;
String s2=(String) o2;
return -s1.compareTo(s2);
}
//return 0;
throw new RuntimeException("输入的数据类型不一致");
}
});
System.out.println(Arrays.toString(arr));
}
@Test
public void test3(){
}
}
该例子的作用就是通过比较数组中元素的大小从而对其进行排序
4,商品类
以下是关于商品类的格式和内容
public class Goods implements Comparable {
private String name;
private double price;
public Goods(){
}
public Goods(String name,double price) {
this.name=name;
this.price = price;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public double getPrice(){
return price;
}
public void setPrice(double price){
this.price=price;
}
public String toString(){
return "Goods{"+
"name='"+name+'\''+
",price="+price+
'}';
}
//指明商品比较大小的方式:按照价格从低到高排序
@Override
public int compareTo(Object o) {
if(o instanceof Goods){
Goods goods=(Goods)o;
if(this.price>goods.price){
return 1;
} else if (this.price<goods.price) {
return -1;
}else {
return 0;
}
//方式二:
//Double.compare(this.price,goods.price);
}
throw new RuntimeException("传入的数据类型不一致!");
}
}
5,其他类
除了以上这些常用类,还有其他常用类比如说Sy,Math,BigInteger和BigDecimal等