java正则判断ip 日期 时间格式是否正确

// 必须定义 `ShowMeBug` 入口类和 `public static void main(String[] args)` 入口方法
import java.util.Date;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class ShowMeBug {
  public static final String KEY_USERNAME = "username";
  
 	public static boolean isIpAddress(String value) {
		Pattern pattern = Pattern.compile("^((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5]"
				+ "|[*])\\.){3}(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5]|[*])$");
		return pattern.matcher(value).matches();
	}
	public int delete() {
		return 0;
	}
  
  public static boolean checkDate(String year, String month, String day) {
		boolean ret = false;
		try {
			String mRegex = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
			Pattern mPattern = Pattern.compile(mRegex);
			Matcher mMatcher = mPattern.matcher(year + "-" + month + "-" + day);
			ret = mMatcher.matches();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}
  	public static int checkDate(String year, String day) {
		int ret = 0;
		try {
			if((Integer.parseInt(year)%4) == 0) {
				if(Integer.parseInt(day) > 29) {
					ret = 1;
				}
			} else {
				if(Integer.parseInt(day) > 28) {
					ret = 2;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}
		public static boolean checkTime(String hour, String minute) {
		boolean ret = false;
		try {
			if(hour.equals("") || minute.equals("")){
				ret = false;
			} else if((Integer.parseInt(hour) >= 0) && (Integer.parseInt(hour) <= 23) && (Integer.parseInt(minute) >= 0) && (Integer.parseInt(minute) <= 59)) {
				ret = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}
public static void main(String[] args) {
String default_modify_time = null;

		Date date = new Date();
		java.text.DateFormat format2 = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
		default_modify_time = format2.format(date);
    System.out.println(default_modify_time);
 
    StringBuilder builder = new StringBuilder("write_raw logo ");
    builder.append("default ");
    if(isIpAddress("192.168.11.1"))
    System.out.println(builder.toString());
   if(checkDate("2012","999") != 0)
         System.out.println("hello,world");
   if(checkTime("12","11"))
       System.out.println("hello,world2");  
  }
}

posted on   lydstory  阅读(194)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2020-09-21 云家庭监控
2020-09-21 关键字:master
2020-09-21 巨鲨医疗
2020-09-21 海思3531
2019-09-21 源代码加密
2019-09-21 gdiplus
2019-09-21 uninstall正确读法

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示