05 object.日期,正则,05object日期
05 object.日期,正则,05object日期
1.子类中重写toString方法:
以显示想要获得的.有意义的字符串
当输出一个对象时, 默认就是输出该对象的toString内容
2.快捷键
查看该类中所有的成员 : ctrl+o.
快捷重写toString方法 : alt+shift+s 点toString()
打开源码搜索框 : ctrl+shift+t 可以快捷看源码
快捷重写equals方法 : alt+shift+s 点hashCode()andequals()
3.在子类中重写其equals方法
4.system类
* System:包含一些有用的类字段和方法。它不能被实例化。
*System.arraycopy(src, 2, dest, 0, 3); 复制数组
(源数组,开始复制索引位置, 目标数组 , 接收元素的索引位置 , 元素的个数)
*long now = System.currentTimeMillis(); 返回当前系统时间
*System.exit(0); 终止虚拟机的运行
日期相关类
5.Date类
Date类的构造方法
Date d1 =new Date() :创建的是一个表示当前系统时间的Date对象
Date d2 =new Date(long date) :根据"指定时间"创建Date对象
System.out.println(d2.toLocaleString());输出格式2017-7-2121:20:28
* Date的常用用方法
毫秒值转Date:两种方式
date.setTime(1000*60*60*24*2);
Date d2 =new Date(1000*60*60*24*2)
Date 转毫秒值 :
date.getTime();
Date d = new Date();// 创建默认当前系统时间
//d.setTime(1000 * 60 * 60 * 24 * 2);
System.out.println(d.toLocaleString());
System.out.println(d.getTime());//172800000
d.setTime(172800000L);
System.out.println(d.toLocaleString());
6.DateFormat类 & SimpleDateFormat
DateFormat是抽象类,我们需要使用其子类SimpleDateFormat来创建对象。
* SimpleDateFormat:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
* 格式化:Date --- String 转化为String
* String s =sdf.format(date);
* 解析:String --- Date 转化为Date
* Date d = sdf.parse("49年9月26日 下午1:29");
* 构造方法:
* SimpleDateFormat() :使用默认的模式进行对象的构建
* SimpleDateFormat("yyyy年MM月dd日"):使用的指定的模式进行对象的构建
*
* 注意:Exceptionin thread "main" java.text.ParseException: Unparseable date: "49年9月26日 下午1:29"
* 解析的字符串,模式必须和构建对象的模式一样(也就是将Date转成String时Date格式可以任意,但是要将String转成Date的时候String要满足SimpleDateFormat的构造格式)
//4个小姨2个大美眉和2个小弟弟
SimpleDateFormat sdf = new SimpleDateFormat();//使用默认模式进行对象的构建
Date date = new Date(); //创建日期对象//49-8-26下午1:29
//使用指定的模式进行对象的构建//1999年9月1日
SimpleDateFormatsdf = new SimpleDateFormat("yyyy年MM月dd日");
Date date =new Date(); //格式化 //2049年08月26日
//使用的指定的模式进行对象的构建
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
Date date = new Date(); //2049年08月26日13:39:12
//格式化
String s = sdf.format(date);
System.out.println(s);//2049年08月26日13:39:12
//解析
Date d = sdf.parse("2049年08月26日13:39:12");
System.out.println(d.toLocaleString());
7.Calendar类
Calendar为抽象类,Calendar类在创建对象时并非直接创建,而是通过静态方法创建,
Calendar c = Calendar.getInstance(); //创建对象,返回当前时间
Calendar类常用方法
* Calendar:日历,提供了一些操作年月日时的方法
* 获取 *修改 *添加
Calendar c = Calendar.getInstance(); 创建当前日历的对象,是通过子类返回的对象
int year = c.get(Calendar.YEAR); 获取年-月-日
int month = c.get(Calendar.MONTH) + 1;注意:月份是从0-11所以要加1
int day = c.get(Calendar.DAY_OF_MONTH);
c.set(Calendar.DAY_OF_MONTH,20);把指定的字段修改成指定的值
c.add(Calendar.DAY_OF_MONTH, -1); 在指定的字段上添加上指定的值
c.set(Calendar.MONTH, 2);
注意:一月对应0,二月对应1,依次推,所以设置3月应该传入2
同时注意设置日历的代码格式
int xinqi = calendar.get(calendar.DAY_OF_WEEK)-1;
注意星期的默认输出是比如今天周1,但打印的是周2,所以要减一天
将日期对象转换为日历对象,
SimpleDateFormatsdf = new SimpleDateFormat("yyyy-MM-dd");
Date d1 = sdf.parse(s);
Calendar c1 = Calendar.getInstance();
c1.setTime(d1); //传入Date对象直接将日期对象转为日历对象
Calendar calendar = sdf.getCalendar();
calendar.get(calendar.DAY_OF_YEAR); 获取日历日期是该年的第几天.
System.out.println(c1.get(calendar.YEAR)+ "年" + month + "月" + day + "日 是星期" + xinqi + " ,是" + year + "年的第" + calendar.get(calendar.DAY_OF_YEAR) +" 天");
包装类
Integer:
* String--- int
* 方式1:int intValue()
Integer i = new Integer("10");
System.out.println(i); // 10
int a = i.intValue();
* 方式2:
intb = Integer.parseInt("20");
* int--- String
* 方式1: +""
String s =15151+"";
* 方式2:StringtoString()
Integer i2 = newInteger(40);
String s =i2.toString();
或者String s2 =Integer.toString(50);
正则表达式
正则表达式:就是一套规则,可以用于匹配字符串
boolean matches(String regex) :判断当前字符串是否匹配指定的正则表达式,如果匹配则返回true,否则返回false
*
匹配QQ : boolean flag = qq.matches("[1-9][0-9]{4,14}");
手机号正则:
\\+如果只有一个+则表示,依次或者多次,直接给切碎了
相关文章
- 暂无相关文章
用户点评