导航:首页 > 编程语言 > java字符串是否是日期

java字符串是否是日期

发布时间:2022-04-26 08:27:01

java 校验字符串是不是日期格式

可以通过正则表达式控制,或者用字符串截取,前4位为年份,是否符合,再截取后面两位是否小于12,再截取最后两位是否小于31,以日期之间的“-”截取,不过这样判断大月小月的日期可能会复杂点,个人认为还是正则表达式更简单。

⑵ java检查字符串是否是合法的日期

ublic static boolean isValidDate(String str) { boolean convertSuccess=true;// 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写; SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm"); try {// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01 format.setLenient(false); format.parse(str); } catch (ParseException e) { // e.printStackTrace();// 如果throw java.text.ParseException或者NullPointerException,就说明格式不对 convertSuccess=false; } return convertSuccess;}

⑶ java用正则表达式判断字符串是不是时间

具体代码如下:

1 public static boolean isValidDate(String str) {
2 boolean convertSuccess=true;
3 // 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;
4 SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm");
5 try {
6 // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
7 format.setLenient(false);
8 format.parse(str);
9 } catch (ParseException e) {
10 // e.printStackTrace();
11 // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
12 convertSuccess=false;
13 }
14 return convertSuccess;

⑷ java判断字符串是否是日期

Java为了支持多语言,没有固定的日期格式。你需要根据自己的需要指定日期格式,然后用DateFormat类或者SimpleDateFormat类来判断是否是正确的日期格式。
下面的例子供参考。更详细的内容可以参考javadoc。

public class DateUtil{ private static final SimpleDateFormat dateFormat = null; static { dateFormat = new SimpleDateFormat("yyyy/MM/dd"); dateFormat.setLenient(false); } public static boolean isValidDate(String s) { try { dateFormat.parse(s); return true; } catch (Exception e) { return false; } } public static String formatDate(Date d) { return dateFormat.format(d); } }

⑸ java怎么判断输入的字符串是否为时间格式

通过正则表达式判断。代码如下:
public boolean isDate(String date) {
String path="\\d{4}-\\d{2}-\\d{2}";//定义匹配规则
Pattern p=Pattern.compile(path);//实例化Pattern
Matcher m=p.matcher(date);//验证字符串内容是否合法
if(m.matches()){
return true;
}
return false;
}

⑹ 如何判断java 一个String字符串里有没有日期并且修改成指定格式

可以使用正则表达式进行匹配替换,例如下面代码:

publicclassApp50{

publicstaticvoidmain(String[]args){

Stringdemo="这个日期是2018/5/6书写出来的";

//xxxx年xx月xx日格式
Stringdemo2=demo.replaceAll("(\d{4})/(\d{1,2})/(\d{1,2})","$1年$2月$3日");
System.out.println(demo2);

//xxxx-xx-xx格式
demo2=demo.replaceAll("(\d{4})/(\d{1,2})/(\d{1,2})","$1-$2-$3");
System.out.println(demo2);
}
}

运行结果:

这个日期是2018年5月6日书写出来的

这个日期是2018-5-6书写出来的

⑺ JAVA如何判断一个字符串是不是一个合法的日期格式

Java为了支持多语言,没有固定的日期格式。你需要根据自己的需要指定日期格式,然后用DateFormat类或者SimpleDateFormat类来判断是否是正确的日期格式。

下面的例子供参考。更详细的内容可以参考javadoc。

publicclassDateUtil
{
=null;
static
{
dateFormat=newSimpleDateFormat("yyyy/MM/dd");
dateFormat.setLenient(false);
}

(Strings)
{
try
{
dateFormat.parse(s);
returntrue;
}
catch(Exceptione)
{
returnfalse;
}
}

publicstaticStringformatDate(Dated)
{
returndateFormat.format(d);
}

}

⑻ 判断java 日期字符串

你可以先设置格式,然后根据“2010/12/06
14:30:30”得到Date格式的日期,如果能得到就可以返回一个Date对象,否则返回null
String
text="2010/12/06
14:30:30";
String
date="yyyy/MM/dd
HH:mm:ss";//HH表示为24小时制,hh表示为12小时制
Date
d=new
SimpleFormatDate(date).parse(text);
if(d==null){
...
}else{
...
}

⑼ java 编程从屏幕输入8-10位字符串,判断是否为日期

publicstaticvoidmain(String[]args)
{
StringcheckValue="2000/03/31";
checkValue=checkValue.replaceAll("/","")+"000000";
Stringyear=checkValue.substring(0,4);//获取年份
Stringmonth=checkValue.substring(4,6);//获取月份
BooleanisLeap=leapYear(Integer.parseInt(year));//判断闰年
System.out.println(isLeap);
StringBuffereL=newStringBuffer();
StringlongMonth="01030507081012";//31天的月份
Stringfix="([2][0-3]|[0-1][0-9]|[1-9])[0-5][0-9]([0-5][0-9]|[6][0])";


if(isLeap&&month.equals("02")){//针对2月份的情况【闰年】
eL.append("\d{4}([1][0-2]|[0][0-9])([2][0-1]|[1-2][0-9]|[0][1-9]|[1-9])"+fix);
}elseif(!isLeap&&month.equals("02")){//针对2月份的情况【非闰年】
eL.append("\d{4}([1][0-2]|[0][0-9])([2][0-1]|[1-2][0-8]|[0][1-9]|[1-9])"+fix);
}elseif(longMonth.contains(month)){//31天月份
eL.append("\d{4}([1][0-2]|[0][0-9])([3][0-1]|[1-2][0-9]|[0][1-9]|[1-9])"+fix);
}else{//30天月份
eL.append("\d{4}([1][0-2]|[0][0-9])([3][0]|[1-2][0-9]|[0][1-9]|[1-9])"+fix);
}
Patternp=Pattern.compile(eL.toString());
Matcherm=p.matcher(checkValue);
booleanflag=m.matches();
if(flag)
{
System.out.println("格式正确");
}
else
{
System.out.println("格式错误");
}

}
publicstaticbooleanleapYear(intyear){
BooleanisLeap=false;
if(((year%100==0)&&(year%400==0))
||((year%100!=0)&&(year%4==0)))
isLeap=true;
returnisLeap;
}

阅读全文

与java字符串是否是日期相关的资料

热点内容
python列表求交集 浏览:872
解压包如何转音频 浏览:447
机明自动编程软件源码 浏览:325
php端口号设置 浏览:540
phperegreplace 浏览:320
androidgridview翻页 浏览:537
ssh协议编程 浏览:634
如何开我的世界电脑服务器地址 浏览:861
玄关pdf 浏览:609
程序员学习论坛 浏览:940
程序员的毒鸡汤怎么做 浏览:548
安卓怎么降级软件到手机 浏览:281
云与服务器入门书籍推荐产品 浏览:636
delphi编程助手 浏览:762
电脑遇到服务器问题怎么办 浏览:515
加工中心编程结束方法 浏览:296
了解什么是web服务器 浏览:140
面向对象的编程的基本特征 浏览:718
php定时执行任务linux 浏览:787
php数组中删除元素 浏览:725