2012. 10. 26.

Java] Date Format 사용하기



출처 : http://www.rgagnon.com/javadetails/java-0099.html


Using DateFormat

import java.text.*;



public class DateUtils {

  public static boolean isValidDateStr(String date) {

    try {

     DateFormat df =

       DateFormat.getDateInstance

         (DateFormat.SHORT); // YYYY-MM-DD

     df.setLenient(false);   // this is important!

     df.parse(date);

    }

    catch (ParseException e) {

     return false;

    }

    catch (IllegalArgumentException e) {

     return false;

    }

    return true;

  }

  

  public static void main(String[] args) {

    System.out.println(" 1900-12-13 valid ? " 

        + DateUtils.isValidDateStr("1900-12-13"));



    // "1990-12/13" throws a ParseException

    System.out.println(" 1900-12/13 valid ? " 

        + DateUtils.isValidDateStr("1900-12/13"));

    // "1990-13-12" throws a IllegalArgumentException

    System.out.println(" 1900-13-12 valid ? " 

        + DateUtils.isValidDateStr("1900-13-12"));

    /*

     * output :

     *  1900-12-13 valid ? true

     *  1900-12/13 valid ? false

     *  1900-13-12 valid ? false

     */

  }

}
Using SimpleDateFormat
package com.rgagnon.howto;



import java.text.*;



public class DateUtils {

  public static boolean isValidDateStr(String date, String format) {

    try {

      SimpleDateFormat sdf = new SimpleDateFormat(format);

      sdf.setLenient(false);

      sdf.parse(date);

    }

    catch (ParseException e) {

      return false;

    }

    catch (IllegalArgumentException e) {

      return false;

    }

    return true;

    }

  

  public static void main(String[] args) {

    System.out.println(" 1900-12-13 valid ? " 

        + DateUtils.isValidDateStr("1900-12-13","yyyy-MM-dd"));



    // "1990-12/13" throws a ParseException

    System.out.println(" 1900-12/13 valid ? " 

        + DateUtils.isValidDateStr("1900-12/13","yyyy-MM-dd"));

    // "1990-13-12" throws a IllegalArgumentException

    System.out.println(" 1900-13-12 valid ? " 

        + DateUtils.isValidDateStr("1900-13-12","yyyy-MM-dd"));

    /*

     * output :

     *  1900-12-13 valid ? true

     *  1900-12/13 valid ? false

     *  1900-13-12 valid ? false

     */

  }

}

Using GregorianCalendar

import java.util.*;



public class jtest {

    public static void main(String args[]) {

        try {

            GregorianCalendar gc = new GregorianCalendar();

            gc.setLenient(false);        // must do this

            gc.set(GregorianCalendar.YEAR, 2003);

            gc.set(GregorianCalendar.MONTH, 42);// invalid month

            gc.set(GregorianCalendar.DATE, 1);



            gc.getTime(); // exception thrown here

        }

        catch (Exception e) {

            e.printStackTrace();

        }

    }

}




출처 :  http://www.dreamincode.net/forums/topic/14886-date-validation-using-simpledateformat/


// date validation using SimpleDateFormat

// it will take a string and make sure it's in the proper 

// format as defined by you, and it will also make sure that

// it's a legal date



public boolean isValidDate(String date)

{

    // set date format, this can be changed to whatever format

    // you want, MM-dd-yyyy, MM.dd.yyyy, dd.MM.yyyy etc.

    // you can read more about it here:

    // http://java.sun.com/j2se/1.4.2/docs/api/index.html

    

    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

    

    // declare and initialize testDate variable, this is what will hold

    // our converted string

    

    Date testDate = null;



    // we will now try to parse the string into date form

    try

    {

      testDate = sdf.parse(date);

    }



    // if the format of the string provided doesn't match the format we 

    // declared in SimpleDateFormat() we will get an exception



    catch (ParseException e)

    {

      errorMessage = "the date you provided is in an invalid date" +

                              " format.";

      return false;

    }



    // dateformat.parse will accept any date as long as it's in the format

    // you defined, it simply rolls dates over, for example, december 32 

    // becomes jan 1 and december 0 becomes november 30

    // This statement will make sure that once the string 

    // has been checked for proper formatting that the date is still the 

    // date that was entered, if it's not, we assume that the date is invalid



    if (!sdf.format(testDate).equals(date)) 

    {

      errorMessage = "The date that you provided is invalid.";

      return false;

    }

    

    // if we make it to here without getting an error it is assumed that

    // the date was a valid one and that it's in the proper format



    return true;



} // end isValidDate

댓글 없음:

댓글 쓰기