1. simpledateformat 日期格式化类
simpledateformat 是java中用于格式化和解析日期的类。它允许你将日期对象转换为特定格式的字符串,或者将特定格式的字符串转换为日期对象。
1.1 date 类型转 string
public final format(date date);
方法传入一个 date 类型的变量,返回一个字符串;
// 指定要转化成哪种格式的字符串
// 第一种(最常用的一种):年-月-日 时-分-秒格式。 为24小时制,例如下午一点,显示为 13-00-00
simpledateformat simpledateformat1 = new simpledateformat("yyyy-mm-dd hh:mm:ss");
// 第二种: 年-月-日 时-分-秒格式。 为12小时制,例如下午一点,就会显示 01-00-00
simpledateformat simpledateformat2 = new simpledateformat("yyyy-mm-dd hh:mm:ss");
// 第三种: 年-月-日 格式。
simpledateformat simpledateformat3 = new simpledateformat("yyyy-mm-dd");
// 获取当前时间。 实际开发则从数据库查询获得,或由前端传入获取
date date = new date();
// 将当前时间格式化成指定格式的字符串
string formatdate1 = simpledateformat1.format(date);
system.out.println("yyyy-mm-dd hh:mm:ss 格式转化为字符串:"+ formatdate1);
string formatdate2 = simpledateformat2.format(date);
system.out.println("yyyy-mm-dd hh:mm:ss 格式转化为字符串:"+ formatdate2);
string formatdate3 = simpledateformat3.format(date);
system.out.println("yyyy-mm-dd 格式转化为字符串:"+ formatdate3);运行代码,如下所示

1.2 string 类型转 date
public date parse(string source) throws parseexception;
方法传入一个字符串,返回一个 date 类型的对象;
// 指定要转化成哪种格式的字符串
// 第一种(最常用的一种):年-月-日 时-分-秒格式。 为24小时制,例如下午一点,显示为 13-00-00
simpledateformat simpledateformat1 = new simpledateformat("yyyy-mm-dd hh:mm:ss");
// 第二种: 年-月-日 时-分-秒格式。 为12小时制,例如下午一点,就会显示 01-00-00
simpledateformat simpledateformat2 = new simpledateformat("yyyy-mm-dd hh:mm:ss");
// 第三种: 年-月-日 格式。
simpledateformat simpledateformat3 = new simpledateformat("yyyy-mm-dd");
string datestring1 = "2012-12-12 12:12:12";
string datestring2 = "2022-12-12 12:12:12";
string datestring3 = "2032-12-12";
try {
date date1 = simpledateformat1.parse(datestring1);
date date2 = simpledateformat2.parse(datestring2);
date date3 = simpledateformat3.parse(datestring3);
system.out.println(date1);
system.out.println(date2);
system.out.println(date3);
} catch (parseexception e) {
throw new runtimeexception(e);运行上述代码,如下所示

2. calendar 日历类
calendar类是java中用于操作日期和时间的抽象类。它提供了一些方法来获取和设置日期和时间,以及进行日期和时间的计算。
(1) getinstance():获取一个表示当前日期和时间的calendar实例;
(2) get(int field):获取指定字段的值。字段可以是calendar.year、calendar.month、calendar.day_of_month等;但这里要注意,月份是从0~11,因此当我们要转化成具体的某一个月时,需要+1;
(3) gettime():将 calendar 对象转换为 date 对象;
(4) settime(date date):将 date 对象设置为 calendar 对象的日期和时间;
calendar calendar = calendar.getinstance();
system.out.println("calender 对象:" +calendar.gettime());
// 获取年份
int year = calendar.get(calendar.year);
system.out.println("获取 calender 对象的年份" +year);
// 获取月份
int month = calendar.get(calendar.month);
system.out.println("获取 calender 对象的月份,未+1" +month);
// 获取日期
int dayofmonth = calendar.get(calendar.day_of_month);
system.out.println("获取 calender 对象的日期" +dayofmonth);
// 打印当前日期和时间,月份进行+1
system.out.println(year + "-" + (month + 1) + "-" + dayofmonth);
// 转化为 date 对象
date date = calendar.gettime();
system.out.println("转化为 date 类型对象:"+ date);运行上述代码如下所示

3. 正则表达式
正则表达式大家都不陌生,虽然对于初学者来说写起来不太简单,但用起来是真的香!
通常都是将正则条件写成一个字符串,然后在去调用 string 字符串类中已经写好的 "matches" 正则方法,方法会返回一个布尔类型的值。
3.1 正则表达式的组成部分
(1)字符类:用于匹配特定类型的字符。例如 [abc] 匹配字符 a,b,c;[a-z] 则表示匹配26个小写字母,[a-z] 则表示匹配26个大写字母,[0-9] 表示匹配0~9的数字;
(2)量词:用于指定匹配的字符数量。例如 a* 匹配零个或多个,a+ 表示匹配一个或多个,a? 表示匹配零个或一个;
(3)锚点:用于指定匹配的位置。例如 "^"表示匹配的开始,“”
(4)分组:分组用于将多个字符或模式组合在一起。例如(abc)匹配 abc,(abc|def)匹配 abc 或 def。
(5)转义字符:转义字符用于匹配特殊字符。例如 \d 匹配任何数字,\w 匹配任何字母,数字或下划线。
(6)预查:预查用于指定匹配的模式必须满足的条件,但不包括匹配结果中。例如 (?=abc)表示匹配的位置后面必须是 abc。
当然了,上述六个是正则表达式的基本组成部分,还有很多高级的组成部分,包括反向引用,非获取组,注释等。
3.2 手机号正则表达式
想要校验一个手机号是否合法,首先要知道手机号的基本构成。这里扩展一个冷知识。
(1)手机号都是以 "1" 开头,这个大家都知道;
(2)手机号码第二位不可能是 "0","1","2";第三可以是任意数字;后面8位也是随机数字;
(3)中国三大运行商,移动,联通,电信分别运营不同的特有号码片段,正是通过第二位和第三位数字来区分属于哪个运营商的。
代码如下
// ^:表示字符串的开始。
// 1:表示手机号的第一位必须是1。
// [3456789]:表示手机号的第二位必须是3、4、5、6、7、8、9中的一个。
// \\d{9}:表示手机号的后面9位必须是数字。第一个 "\" 表示转义符
// $:表示字符串的结束。
string regex = "^1[3456789]\\d{9}$";
// 现在手机号直接定义一个,实际开发会由前端传给我们
string phonenumber = "13812345678";
if (phonenumber.matches(regex)) {
system.out.println("手机号格式正确");
} else {
system.out.println("手机号格式错误");
}3.3 常用密码校验正则表达式
密码就没啥可说的了,安全性高一些的网站或者app,可能会强制要求至少包含一个大小写、数字、特殊字符,且不能含有空格,长度通常为8~16位。
// 至少包含一个大写字母:(?=.*[a-z])
// 至少包含一个小写字母:(?=.*[a-z])
// 至少包含一个数字:(?=.*\\d)
// 至少包含一个特殊字符:(?=.*[!@#$%^&*()_+\\-=\\[\\]{};':"\\|,.<>\\/?])
// 不允许有空格:^[^\\s]+$
// 至少8个字符且不多于16个字符:^.{8,}$
string regex = "^(?=.*[a-z])(?=.*[a-z])(?=.*\\d)(?=.*[!@#$%^&*()_+\\-=\\[\\]{};':\"\\|,.<>\\/?]).{8,16}$";
string password = "abc123!@#";
if (password.matches(regex)) {
system.out.println("密码符合要求");
} else {
system.out.println("密码不符合要求");
}到此这篇关于java常用 date 时间格式化、calender日历、正则表达式的用法的文章就介绍到这了,更多相关java date时间格式化内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论