JAVA日期的判断

Python020

JAVA日期的判断,第1张

import java.text.*

import java.util.*

public class isWeekSame

{

//方法名称:isSameDate(String date1,String date2)

//功能描述:判断date1和date2是否在同一周

//输入参数:date1,date2

//输出参数:

//返 回 值:false 或 true

//其它说明:主要用到Calendar类中的一些方法

public static boolean isSameDate(String date1,String date2)

{

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd")

Date d1 = null

Date d2 = null

try

{

d1 = format.parse(date1)

d2 = format.parse(date2)

}

catch(Exception e)

{

e.printStackTrace()

}

Calendar cal1 = Calendar.getInstance()

Calendar cal2 = Calendar.getInstance()

cal1.setTime(d1)

cal2.setTime(d2)

int subYear = cal1.get(Calendar.YEAR)-cal2.get(Calendar.YEAR)

//subYear==0,说明是同一年

if(subYear == 0)

{

if(cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))

return true

}

//例子:cal1是"2005-1-1",cal2是"2004-12-25"

//java对"2004-12-25"处理成第52周

// "2004-12-26"它处理成了第1周,和"2005-1-1"相同了

//大家可以查一下自己的日历

//处理的比较好

//说明:java的一月用"0"标识,那么12月用"11"

else if(subYear==1 &&cal2.get(Calendar.MONTH)==11)

{

if(cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))

return true

}

//例子:cal1是"2004-12-31",cal2是"2005-1-1"

else if(subYear==-1 &&cal1.get(Calendar.MONTH)==11)

{

if(cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))

return true

}

return false

}

public static void main(String[] args)

{

// TODO Auto-generated method stub

//测试1

boolean a = isSameDate("2005-1-1","2005-1-3")

if(a)

{

System.out.println("2005-1-1和2005-1-3是同一周!")

}

else

{

System.out.println("2005-1-1和2005-1-3不是同一周!")

}

//测试2

boolean b = isSameDate("2005-1-1","2004-12-25")

if(b)

{

System.out.println("2005-1-1和2004-12-25是同一周!")

}

else

{

System.out.println("2005-1-1和2004-12-25不是同一周!")

}

boolean c = isSameDate("2004-12-25","2005-1-1")

if(c)

{

System.out.println("2004-12-25和2005-1-1是同一周!")

}

else

{

System.out.println("2004-12-25和2005-1-1不是同一周!")

}

//测试3

boolean d = isSameDate("2005-1-1","2004-12-26")

if(d)

{

System.out.println("2005-1-1和2004-12-26是同一周!")

}

else

{

System.out.println("2005-1-1和2004-12-26不是同一周!")

}

boolean e = isSameDate("2004-12-26","2005-1-1")

if(e)

{

System.out.println("2004-12-26和2005-1-1是同一周!")

}

else

{

System.out.println("2004-12-26和2005-1-1不是同一周!")

}

}

}

你可以参考下。。

java.text.DateFormat dateFormat= new java.text.SimpleDateFormat("yyyy-MM-dd kk:mm:ss", Locale.ENGLISH)

dateFormat.setLenient(false)

java.util.Date timeDate = dateFormat.parse(dateString)

//转换为util类型

看到dateFormat.setLenient(false)没有,设定其为false就是强制判断是否非法日期,不让系统自动转换,否则2月31号系统会自动转换为3月2号或者3号。

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

public class DateUtil

{

private static final SimpleDateFormat dateFormat = null

static

{

// 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;

dateFormat = new SimpleDateFormat("yyyy/MM/dd")

// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01

dateFormat.setLenient(false)

}

public static boolean isValidDate(String s)

{

try

{

dateFormat.parse(s)

return true

}

catch (Exception e)

{

// 如果throw java.text.ParseException或者NullPointerException,就说明格式不对

return false

}

}

// 下面这个方法则可以将一个日期按照你指定的格式输出

public static String formatDate(Date d)

{

return dateFormat.format(d)

}

}