1. 通过正则表达式判断时间是否符合yyyyMMdd格式
/***
* 判断字符串是否是yyyyMMdd格式
* @param mes 字符串
* @return boolean 是否是日期格式
*/
public static boolean isRqFormat(String mes){
String format = "([0-9]{4})(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])";
Pattern pattern = Pattern.compile(format);
Matcher matcher = pattern.matcher(mes);
if (matcher.matches()) {
pattern = Pattern.compile("(\\d{4})(\\d{2})(\\d{2})");
matcher = pattern.matcher(mes);
if (matcher.matches()) {
int y = Integer.valueOf(matcher.group(1));
int m = Integer.valueOf(matcher.group(2));
int d = Integer.valueOf(matcher.group(3));
if (d > 28) {
Calendar c = Calendar.getInstance();
//每个月的最大天数
c.set(y, m-1, 1);
int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
return (lastDay >= d);
}
}
return true;
}
return false;
}
2.判断字符串是否是yyyyMMddHHmmss格式
/***
* 判断字符串是否是yyyyMMddHHmmss格式
* @param mes 字符串
* @return boolean 是否是日期格式
*/
public static boolean isRqSjFormat(String mes){
String format = "([0-9]{4})(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])"
+ "([01][0-9]|2[0-3])[0-5][0-9][0-5][0-9]";
Pattern pattern = Pattern.compile(format);
Matcher matcher = pattern.matcher(mes);
if (matcher.matches()) {
pattern = Pattern.compile("(\\d{4})(\\d{2})(\\d{2}).*");
matcher = pattern.matcher(mes);
if (matcher.matches()) {
int y = Integer.valueOf(matcher.group(1));
int m = Integer.valueOf(matcher.group(2));
int d = Integer.valueOf(matcher.group(3));
if (d > 28) {
Calendar c = Calendar.getInstance();
c.set(y, m-1, 1); //每个月的最大天数
int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
return (lastDay >= d);
}
}
return true;
}
return false;
}
3.判断字符串是否是HHmmss格式
/***
* 判断字符串是否是HHmmss格式
* @param mes 字符串
* @return boolean 是否是日期格式
*/
public static boolean isSjFormat(String mes){
if(mes.length()!=6){
return false;
}
String regex="^\\d+$";
if(!mes.matches(regex)){
return false;
}
int h;
int m;
int s;
try {
h=Integer.parseInt(mes.substring(0, 2));
m=Integer.parseInt(mes.substring(2,4));
s=Integer.parseInt(mes.substring(4, 6));
if(h>23||h<0||m>59||m<0||s>59||s<0){
return false;
}
} catch (Exception e) {
LoggerUtil.info(mes+"不是HHmmss时间格式的字符串");
return false;
}
return true;
}
格林威治时间转换
package com.cn;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/**
*
*
* @Package: com.cn
* @ClassName: Test
* @Description:TODO
* @author: zuokun
* @date: 2021年1月19日 上午9:52:42
*/
public class Test {
@org.junit.Test
public void getTime() throws ParseException {
String str = "Tue Jan 01 00:00:00 CST 2021";
Date date = parse(str, "EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));
System.out.printf("%tF %<tT%n", date);
}
// 格林威治时间转Date
private Date parse(String str, String pattern, Locale locale) {
if (str == null || pattern == null) {
return null;
}
try {
return new SimpleDateFormat(pattern, locale).parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
参考连接
https://www.cnblogs.com/zktww/p/14296407.html?ivk_sa=1024320u
https://www.cnblogs.com/kiko2014551511/p/11547922.html