導航:首頁 > 編程語言 > java日期是否合法

java日期是否合法

發布時間:2024-06-17 01:30:22

java 判斷一個字元串是不是一個合法的日期格式

(Stringstr){
booleanconvertSuccess=true;
//指定日期格式為四位年/兩位月份/兩位日期,注意yyyy/MM/dd區分大小寫;
SimpleDateFormatformat=newSimpleDateFormat("yyyy/MM/ddHH:mm");
try{
//設置lenient為false.否則SimpleDateFormat會比較寬松地驗證日期,比如2007/02/29會被接受,並轉換成2007/03/01
format.setLenient(false);
format.parse(str);
}catch(ParseExceptione){
//e.printStackTrace();
//如果throwjava.text.ParseException或者NullPointerException,就說明格式不對
convertSuccess=false;
}
returnconvertSuccess;
}

② java涓濡備綍鍒ゆ柇杈撳叆鐨勬棩鏈熸槸鍚﹀悎娉曪紵

import java.util.*;
import java.util.regex.*;
import java.text.*;

/** 榪欎釜鏄鎸夌収妤間富鐨勬弿榪頒嬌鐢ㄩ氳繃鍒ゆ柇瀛楃﹂獙璇佹椂闂村悎娉曟 */
public class DateUtils2 {

//嫻嬭瘯浠g爜 begin
public static void main(String[] s){
//浠ヤ笅鏄嫻嬭瘯浠g爜
test("20099-1-1");
test("20099-100-1");
test("20099-1-100");
test("2009-1-1");
test("2009-1-31");
test("2009-2-28");
test("2009-2-29");
test("2008-2-29");
}

private static void test(String stringdate){
System.out.println("杈撳叆[" + stringdate + "]鏄鍚﹀悎娉:" + validate(stringdate));
}
//嫻嬭瘯浠g爜 end

//==

/** 鍒ゆ柇涓繪柟娉 */
public static boolean validate(String dateString){
//浣跨敤姝e垯琛ㄨ揪寮 嫻嬭瘯 瀛楃 絎﹀悎 dddd-dd-dd 鐨勬牸寮(d琛ㄧず鏁板瓧)
Pattern p = Pattern.compile("\\d{4}+[-]\\d{1,2}+[-]\\d{1,2}+");
Matcher m = p.matcher(dateString);
if(!m.matches()){ return false;}

//寰楀埌騫存湀鏃
String[] array = dateString.split("-");
int year = Integer.valueOf(array[0]);
int month = Integer.valueOf(array[1]);
int day = Integer.valueOf(array[2]);

if(month<1 || month>12){ return false;}
int[] monthLengths = new int[]{0, 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(isLeapYear(year)){
monthLengths[2] = 29;
}else{
monthLengths[2] = 28;
}
int monthLength = monthLengths[month];
if(day<1 || day>monthLength){
return false;
}
return true;
}

/** 鏄鍚︽槸闂板勾 */
private static boolean isLeapYear(int year){
return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ;
}
}

鐨 鍒氬ソ鍜屼綘鐨勯棶棰樹竴鏍鳳綖鍀烇綖鍀烇綖鍀烇綖

③ java如何對生日(yyyymmdd)進行合法性判斷

修改你說的存在的bug
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TestBirthday {
public static void main(String[] args){
String birthday="20130132";
System.out.println(isBirthday(birthday));
}
/*
* @param : birthday 傳入一個日期格式的字元串
* 支持 yyyy-MM-dd ,yyyyMMdd MM-dd-yyyy ,yyyy年MM月dd日
* @isBirthday 判斷輸入的字元串是否是合法的生日 生日不能大於當前日期
* */
public static boolean isBirthday(String birthday){
//定義需要過濾的條件,可以將你期望的日期格式添加到數組中
String[] formats={
"yyyy-MM-dd",
"yyyyMMdd",
"MM-dd-yyyy",
"yyyy年MM月dd日"
};
/*
* 設置格式過濾器
*/
//記錄傳入的日期字元串,轉換成日期類型
Date birth=null;
//判斷格式是否正確,默認值為false
boolean isRight=false;
for(String f:formats){
try {
birth =new SimpleDateFormat(f).parse(birthday);
//校驗日期轉換後是和傳入的值不相同,說明日期傳入有問題
//修正樓上提到的bug
if(!new SimpleDateFormat(f).format(birth).equals(birthday)){
return false;
}
isRight=true;
break;
} catch (ParseException e) {}
}
if(isRight){
//獲取當前日期的毫秒數
long now =new Date().getTime();
//獲取生日的毫秒數
long birthTime = birth.getTime();
//如果當前時間小於生日,生日不合法。反之合法
return birthTime<=now;
}else{
//輸入的參數類型不是日期類型,或者類型和過濾中設置的類型不匹配
return false;
}
}
}

④ 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日期是否合法相關的資料

熱點內容
php執行linux命令 瀏覽:790
安卓解壓軟體有哪些 瀏覽:51
午馬影院 瀏覽:275
電腦文件夾為什麼是一個圓形 瀏覽:113
程序員都是怎麼樣在letcode刷題的 瀏覽:672
程序員用的貓的軟體叫啥 瀏覽:197
福昕pdf拆分 瀏覽:890
android獲取存儲卡 瀏覽:349
安卓機一直卡怎麼辦 瀏覽:782
榆社電影院放映表 瀏覽:130
程序員增肥 瀏覽:18
13排imax坐第幾排最好 瀏覽:568
天津單片機培訓 瀏覽:82
小女孩功夫特別厲害電影 瀏覽:963
d3jspython 瀏覽:132
python開源前端查詢資料庫 瀏覽:566
如何用數軸進行編譯 瀏覽:29
logo控制器加密 瀏覽:951
圖示對稱加密 瀏覽:817
在線播放最新網址 瀏覽:103