java 字符串 有效日期_工具类写java中的字符串/日期的验证的方法

这是我写的一个工具类,其中里面的isLagelDateOfString方法可以检查字符串是否是合法的日期

import java.util.Calendar;

import java.util.GregorianCalendar;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

/**

* 提供一些对处理日期实用的静态方法

* @author Freedom

*

*/

public class CalendarKit {

/**作为键使用*/

public static final String YEAR = "year";

public static final String MONTH = "month";

public static final String DAY = "day";

/**日历的起始年份*/

public static final int START_YEAR = 1970;

/**日历的结束年份*/

public static final int END_YEAR = 2089;

/**保存文本描述的星期数*/

public static final String[] DAY_OF_WEEK = {"日", "一", "二", "三", "四", "五", "六"};

/**月份的文本描述*/

public static final String[] MONTHS = {"一月", "二月", "", "四月", "五月",

"六月", "七月", "八月", "九月", "十月", "十一月", "十二月"};

/**保存每个月的天数*/

public static final int[] DAYS_OF_MONTH = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

/**

* 根据d返回一个字符串天数,如果d小于0则在前面加"0"

* @param d

* @return

*/

public static String getDayForString(int d) {

return d 

}

/**

* 根据m返回一个字符串月份,如果m小于0则在前面加"0"

* @param m

* @return

*/

public static String getMonthForString(int m) {

return m 

}

/**

* 检查year是否在有效的年份范围内

* @param year

* @return

*/

public static boolean isLagelYear(int year) {

return year >= START_YEAR && year <= END_YEAR;

}

/**

* 检查month是否在有效的月份范围内

* @param month

* @return

*/

public static boolean isLagelMonth(int month) {

return month >= 1 && month <= 12;

}

/**

* 检查天数是否在有效的范围内,因为天数会根据年份和月份的不同而不同

* 所以必须依赖年份和月份进行检查

* @param year

* @param month

* @param day

* @return

*/

public static boolean isLagelDay(int year, int month, int day) {

if(!isLagelYear(year)) { return false; }

if(!isLagelMonth(month)) { return false; }

if(month == 2 && isLeapYear(year)) {//且是2月份

return day >= 1 && day <= 29;

}

return day >= 1 && day <= DAYS_OF_MONTH[month - 1];//其他月份

}

/**是否是*/

public static boolean isLeapYear(int year) {

return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;

}

/**

* 判断该日期是否在有效日期范围内

* 有效日期为:1970-1-1~2089-12-31

* @param date

* @return

*/

public static boolean isLagelDate(GregorianCalendar date) {

int y = date.get(Calendar.YEAR);

int m = date.get(Calendar.MONTH) + 1;

int d = date.get(Calendar.DAY_OF_MONTH);

return isLagelDay(y, m, d);

}

/**

* 返回date的一号是星期几

* @param date

* @return

*/

public static int getWeekForFirst(GregorianCalendar date) {

int day = date.get(Calendar.DAY_OF_MONTH);//当前天

int week = date.get(Calendar.DAY_OF_WEEK) - 1;

int t = day % 7 - 1;

int weekOfFirst = week 

return weekOfFirst == 7 ? 0 : weekOfFirst;

}

/**

* 检查以字符串形式的日期格式是否正确

* 如果正确则返回由年,月,日组成的数组

* 否则返回包含三个-1值的数组

* @param date

* @return

*/

public static int[] checkDateForString(String date) {

int[] d = {-1, -1, -1};

Pattern p = Pattern.compile("^\\d{4}-\\d{1,2}-\\d{1,2}$");

Matcher match = p.matcher(date);

if(!match.matches()){ return d; }

String[] str = date.split("-");

d[0] = Integer.parseInt(str[0]);

d[1] = Integer.parseInt(str[1]);

d[2] = Integer.parseInt(str[2]);

return d;

}

/**

* 如果该日期字符串是有效的返回true

* @param date

* @return

*/

public static boolean isLagelDateOfString(String date) {

Pattern p = Pattern.compile("^\\d{4}-\\d{1,2}-\\d{1,2}$");

Matcher match = p.matcher(date);

return match.matches();

}

/**

* 根据年份和月份获取该月的总天数

* 如果年份或月份不合法,则返回-1

* @param year

* @param month

* @return

*/

public static int getDaysCount(int year, int month) {

if(!isLagelYear(year) || !isLagelMonth(month)) {

return -1;

}

int d = isLeapYear(year) && month == 2 ? 1 : 0;

return d += DAYS_OF_MONTH[month - 1];

}

/**

* 根据年份和月份获取该月的总天数

* 如果年份或月份不合法,则返回-1

* @param date

* @return

*/

public static int getDaysCount(GregorianCalendar date) {

int year = date.get(Calendar.YEAR);

int month = date.get(Calendar.MONTH) + 1;

if(!isLagelYear(year) || !isLagelMonth(month)) {

return -1;

}

int d = isLeapYear(year) && month == 2 ? 1 : 0;

return d += DAYS_OF_MONTH[month - 1];

}

/**

* 如果date的当前天数大于otherMonth的总天数则返回true

* 例:date = 2010-3-29  otherMonth = 2(年份也是2010)

* otherMonth的总天数只有28天,所以date的当前天数比它大

* @param date

* @param otherMonth 和date同一个年份的其他月份

* @return

*/

public static boolean daysOfCountByMonth(GregorianCalendar date, int otherMonth) {

int otherDays = getDaysCount(date.get(Calendar.YEAR), otherMonth);

return date.get(Calendar.DAY_OF_MONTH) > otherDays;

}

/**

* 如果date的当前天数大于otherYear的总天数则返回true

* @param date

* @param otherYear 和date同一个月份的其他年份

* @return

*/

public static boolean daysOfCountByYear(GregorianCalendar date, int otherYear) {

int otherDays = getDaysCount(otherYear, date.get(Calendar.MONTH) + 1);

return date.get(Calendar.DAY_OF_MONTH) > otherDays;

}

/**

* 将date用sign分开

* @param date

* @param sign

* @return

*/

public static String formatDate(GregorianCalendar date, String sign) {

String y = date.get(Calendar.YEAR) + "";

String m = CalendarKit.getMonthForString(date.get(Calendar.MONTH) + 1);

String d = CalendarKit.getDayForString(date.get(Calendar.DAY_OF_MONTH));

return y + sign + m + sign + d;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值