文本框中提交日期为什么不能正确识别?一定要使用下拉吗?
oksonic
2009-11-01
文本框中提交日期为什么不能正确识别?一定要使用下拉吗?
|
|
wusema
2009-11-02
一个日期通用类,获得日期用DateUtil.parseDateFormat(params.date,"yyyy-MM-dd hh:mm:ss")
import java.text.*; import java.util.*; public class DateUtil { private static final int[] dayArray = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; private static SimpleDateFormat sdf = new SimpleDateFormat(); public static synchronized Calendar getCalendar() { return GregorianCalendar.getInstance(); } /** @return String */ public static synchronized String getDateMilliFormat() { Calendar cal = Calendar.getInstance(); return getDateMilliFormat( cal ); } /** @param cal @return String */ public static synchronized String getDateMilliFormat( java.util.Calendar cal ) { String pattern = "yyyy-MM-dd HH:mm:ss,SSS"; return getDateFormat( cal, pattern ); } /** @param date @return String */ public static synchronized String getDateMilliFormat( java.util.Date date ) { String pattern = "yyyy-MM-dd HH:mm:ss,SSS"; return getDateFormat( date, pattern ); } /** @param strDate @return java.util.Calendar */ public static synchronized Calendar parseCalendarMilliFormat( String strDate ) { String pattern = "yyyy-MM-dd HH:mm:ss,SSS"; return parseCalendarFormat( strDate, pattern ); } /** @param strDate @return java.util.Date */ public static synchronized Date parseDateMilliFormat( String strDate ) { String pattern = "yyyy-MM-dd HH:mm:ss,SSS"; return parseDateFormat( strDate, pattern ); } /** @return String */ public static synchronized String getDateSecondFormat() { Calendar cal = Calendar.getInstance(); return getDateSecondFormat( cal ); } /** @param cal @return String */ public static synchronized String getDateSecondFormat( java.util.Calendar cal ) { String pattern = "yyyy-MM-dd HH:mm:ss"; return getDateFormat( cal, pattern ); } /** @param date @return String */ public static synchronized String getDateSecondFormat( java.util.Date date ) { String pattern = "yyyy-MM-dd HH:mm:ss"; return getDateFormat( date, pattern ); } /** @param strDate @return java.util.Calendar */ public static synchronized Calendar parseCalendarSecondFormat( String strDate ) { String pattern = "yyyy-MM-dd HH:mm:ss"; return parseCalendarFormat( strDate, pattern ); } /** @param strDate @return java.util.Date */ public static synchronized Date parseDateSecondFormat( String strDate ) { String pattern = "yyyy-MM-dd HH:mm:ss"; return parseDateFormat( strDate, pattern ); } /** @return String */ public static synchronized String getDateMinuteFormat() { Calendar cal = Calendar.getInstance(); return getDateMinuteFormat( cal ); } /** @param cal @return String */ public static synchronized String getDateMinuteFormat( java.util.Calendar cal ) { String pattern = "yyyy-MM-dd HH:mm"; return getDateFormat( cal, pattern ); } /** @param date @return String */ public static synchronized String getDateMinuteFormat( java.util.Date date ) { String pattern = "yyyy-MM-dd HH:mm"; return getDateFormat( date, pattern ); } /** @param strDate @return java.util.Calendar */ public static synchronized Calendar parseCalendarMinuteFormat( String strDate ) { String pattern = "yyyy-MM-dd HH:mm"; return parseCalendarFormat( strDate, pattern ); } /** @param strDate @return java.util.Date */ public static synchronized Date parseDateMinuteFormat( String strDate ) { String pattern = "yyyy-MM-dd HH:mm"; return parseDateFormat( strDate, pattern ); } /** @return String */ public static synchronized String getDateDayFormat() { Calendar cal = Calendar.getInstance(); return getDateDayFormat( cal ); } /** @param cal @return String */ public static synchronized String getDateDayFormat( java.util.Calendar cal ) { String pattern = "yyyy-MM-dd"; return getDateFormat( cal, pattern ); } /** @param date @return String */ public static synchronized String getDateDayFormat( java.util.Date date ) { String pattern = "yyyy-MM-dd"; return getDateFormat( date, pattern ); } /** @param strDate @return java.util.Calendar */ public static synchronized Calendar parseCalendarDayFormat( String strDate ) { String pattern = "yyyy-MM-dd"; return parseCalendarFormat( strDate, pattern ); } /** @param strDate @return java.util.Date */ public static synchronized Date parseDateDayFormat( String strDate ) { String pattern = "yyyy-MM-dd"; return parseDateFormat( strDate, pattern ); } /** @return String */ public static synchronized String getDateFileFormat() { Calendar cal = Calendar.getInstance(); return getDateFileFormat( cal ); } /** @param cal @return String */ public static synchronized String getDateFileFormat( java.util.Calendar cal ) { String pattern = "yyyy-MM-dd_HH-mm-ss"; return getDateFormat( cal, pattern ); } /** @param date @return String */ public static synchronized String getDateFileFormat( java.util.Date date ) { String pattern = "yyyy-MM-dd_HH-mm-ss"; return getDateFormat( date, pattern ); } /** @param strDate @return java.util.Calendar */ public static synchronized Calendar parseCalendarFileFormat( String strDate ) { String pattern = "yyyy-MM-dd_HH-mm-ss"; return parseCalendarFormat( strDate, pattern ); } /** @param strDate @return java.util.Date */ public static synchronized Date parseDateFileFormat( String strDate ) { String pattern = "yyyy-MM-dd_HH-mm-ss"; return parseDateFormat( strDate, pattern ); } /** @return String */ public static synchronized String getDateW3CFormat() { Calendar cal = Calendar.getInstance(); return getDateW3CFormat( cal ); } /** @param cal @return String */ public static synchronized String getDateW3CFormat( java.util.Calendar cal ) { String pattern = "yyyy-MM-dd HH:mm:ss"; return getDateFormat( cal, pattern ); } /** @param date @return String */ public static synchronized String getDateW3CFormat( java.util.Date date ) { String pattern = "yyyy-MM-dd HH:mm:ss"; return getDateFormat( date, pattern ); } /** @param strDate @return java.util.Calendar */ public static synchronized Calendar parseCalendarW3CFormat( String strDate ) { String pattern = "yyyy-MM-dd HH:mm:ss"; return parseCalendarFormat( strDate, pattern ); } /** @param strDate @return java.util.Date */ public static synchronized Date parseDateW3CFormat( String strDate ) { String pattern = "yyyy-MM-dd HH:mm:ss"; return parseDateFormat( strDate, pattern ); } /** @param cal @return String */ public static synchronized String getDateFormat( java.util.Calendar cal ) { String pattern = "yyyy-MM-dd HH:mm:ss"; return getDateFormat( cal, pattern ); } /** @param date @return String */ public static synchronized String getDateFormat( java.util.Date date ) { String pattern = "yyyy-MM-dd HH:mm:ss"; return getDateFormat( date, pattern ); } /** @param strDate @return java.util.Calendar */ public static synchronized Calendar parseCalendarFormat( String strDate ) { String pattern = "yyyy-MM-dd HH:mm:ss"; return parseCalendarFormat( strDate, pattern ); } /** @param strDate @return java.util.Date */ public static synchronized Date parseDateFormat( String strDate ) { String pattern = "yyyy-MM-dd HH:mm:ss"; return parseDateFormat( strDate, pattern ); } /** @param cal @param pattern @return String */ public static synchronized String getDateFormat( java.util.Calendar cal, String pattern ) { return getDateFormat( cal.getTime(), pattern ); } /** @param date @param pattern @return String */ public static synchronized String getDateFormat( java.util.Date date, String pattern ) { synchronized ( sdf ) { String str = null; sdf.applyPattern( pattern ); str = sdf.format( date ); return str; } } /** @param strDate @param pattern @return java.util.Calendar */ public static synchronized Calendar parseCalendarFormat( String strDate, String pattern ) { synchronized ( sdf ) { Calendar cal = null; sdf.applyPattern( pattern ); try { sdf.parse( strDate ); cal = sdf.getCalendar(); } catch ( Exception e ) { } return cal; } } /** @param strDate @param pattern @return java.util.Date */ public static synchronized Date parseDateFormat( String strDate, String pattern ) { synchronized ( sdf ) { Date date = null; sdf.applyPattern( pattern ); try { date = sdf.parse( strDate ); } catch ( Exception e ) { } return date; } } public static synchronized int getLastDayOfMonth( int month ) { if ( month < 1 || month > 12 ) { return -1; } int retn = 0; if ( month == 2 ) { if ( isLeapYear() ) { retn = 29; } else { retn = dayArray[month - 1]; } } else { retn = dayArray[month - 1]; } return retn; } public static synchronized int getLastDayOfMonth( int year, int month ) { if ( month < 1 || month > 12 ) { return -1; } int retn = 0; if ( month == 2 ) { if ( isLeapYear( year ) ) { retn = 29; } else { retn = dayArray[month - 1]; } } else { retn = dayArray[month - 1]; } return retn; } public static synchronized boolean isLeapYear() { Calendar cal = Calendar.getInstance(); int year = cal.get( Calendar.YEAR ); return isLeapYear( year ); } public static synchronized boolean isLeapYear( int year ) { if ( ( year % 400 ) == 0 ) return true; else if ( ( year % 4 ) == 0 ) { if ( ( year % 100 ) == 0 ) return false; else return true; } else return false; } public static synchronized boolean isLeapYear( java.util.Date date ) { // int year = date.getYear(); GregorianCalendar gc = ( GregorianCalendar ) Calendar.getInstance(); gc.setTime( date ); int year = gc.get( Calendar.YEAR ); return isLeapYear( year ); } public static synchronized boolean isLeapYear( java.util.Calendar gc ) { int year = gc.get( Calendar.YEAR ); return isLeapYear( year ); } public static synchronized java.util.Date getPreviousWeekDay( java.util.Date date ) { { GregorianCalendar gc = ( GregorianCalendar ) Calendar.getInstance(); gc.setTime( date ); return getPreviousWeekDay( gc ); // switch ( gc.get( Calendar.DAY_OF_WEEK ) ) // { // case ( Calendar.MONDAY ): // gc.add( Calendar.DATE, -3 ); // break; // case ( Calendar.SUNDAY ): // gc.add( Calendar.DATE, -2 ); // break; // default: // gc.add( Calendar.DATE, -1 ); // break; // } // return gc.getTime(); } } public static synchronized java.util.Date getPreviousWeekDay( java.util.Calendar gc ) { { switch ( gc.get( Calendar.DAY_OF_WEEK ) ) { case ( Calendar.MONDAY ): gc.add( Calendar.DATE, -3 ); break; case ( Calendar.SUNDAY ): gc.add( Calendar.DATE, -2 ); break; default: gc.add( Calendar.DATE, -1 ); break; } return gc.getTime(); } } public static synchronized java.util.Date getNextWeekDay( java.util.Date date ) { GregorianCalendar gc = ( GregorianCalendar ) Calendar.getInstance(); gc.setTime( date ); switch ( gc.get( Calendar.DAY_OF_WEEK ) ) { case ( Calendar.FRIDAY ): gc.add( Calendar.DATE, 3 ); break; case ( Calendar.SATURDAY ): gc.add( Calendar.DATE, 2 ); break; default: gc.add( Calendar.DATE, 1 ); break; } return gc.getTime(); } public static synchronized java.util.Calendar getNextWeekDay( java.util.Calendar gc ) { switch ( gc.get( Calendar.DAY_OF_WEEK ) ) { case ( Calendar.FRIDAY ): gc.add( Calendar.DATE, 3 ); break; case ( Calendar.SATURDAY ): gc.add( Calendar.DATE, 2 ); break; default: gc.add( Calendar.DATE, 1 ); break; } return gc; } public static synchronized java.util.Date getLastDayOfNextMonth( java.util.Date date ) { GregorianCalendar gc = ( GregorianCalendar ) Calendar.getInstance(); gc.setTime( date ); gc.setTime( DateUtil.getNextMonth( gc.getTime() ) ); gc.setTime( DateUtil.getLastDayOfMonth( gc.getTime() ) ); return gc.getTime(); } public static synchronized java.util.Date getLastDayOfNextWeek( java.util.Date date ) { GregorianCalendar gc = ( GregorianCalendar ) Calendar.getInstance(); gc.setTime( date ); gc.setTime( DateUtil.getNextWeek( gc.getTime() ) ); gc.setTime( DateUtil.getLastDayOfWeek( gc.getTime() ) ); return gc.getTime(); } public static synchronized java.util.Date getFirstDayOfNextMonth( java.util.Date date ) { GregorianCalendar gc = ( GregorianCalendar ) Calendar.getInstance(); gc.setTime( date ); gc.setTime( DateUtil.getNextMonth( gc.getTime() ) ); gc.setTime( DateUtil.getFirstDayOfMonth( gc.getTime() ) ); return gc.getTime(); } public static synchronized java.util.Calendar getFirstDayOfNextMonth( java.util.Calendar gc ) { gc.setTime( DateUtil.getNextMonth( gc.getTime() ) ); gc.setTime( DateUtil.getFirstDayOfMonth( gc.getTime() ) ); return gc; } public static synchronized java.util.Date getFirstDayOfNextWeek( java.util.Date date ) { GregorianCalendar gc = ( GregorianCalendar ) Calendar.getInstance(); gc.setTime( date ); gc.setTime( DateUtil.getNextWeek( gc.getTime() ) ); gc.setTime( DateUtil.getFirstDayOfWeek( gc.getTime() ) ); return gc.getTime(); } public static synchronized java.util.Calendar getFirstDayOfNextWeek( java.util.Calendar gc ) { gc.setTime( DateUtil.getNextWeek( gc.getTime() ) ); gc.setTime( DateUtil.getFirstDayOfWeek( gc.getTime() ) ); return gc; } public static synchronized java.util.Date getNextMonth( java.util.Date date ) { GregorianCalendar gc = ( GregorianCalendar ) Calendar.getInstance(); gc.setTime( date ); gc.add( Calendar.MONTH, 1 ); return gc.getTime(); } public static synchronized java.util.Calendar getNextMonth( java.util.Calendar gc ) { gc.add( Calendar.MONTH, 1 ); return gc; } public static synchronized java.util.Date getNextDay( java.util.Date date ) { GregorianCalendar gc = ( GregorianCalendar ) Calendar.getInstance(); gc.setTime( date ); gc.add( Calendar.DATE, 1 ); return gc.getTime(); } public static synchronized java.util.Calendar getNextDay( java.util.Calendar gc ) { gc.add( Calendar.DATE, 1 ); return gc; } public static synchronized java.util.Date getNextWeek( java.util.Date date ) { GregorianCalendar gc = ( GregorianCalendar ) Calendar.getInstance(); gc.setTime( date ); gc.add( Calendar.DATE, 7 ); return gc.getTime(); } public static synchronized java.util.Calendar getNextWeek( java.util.Calendar gc ) { gc.add( Calendar.DATE, 7 ); return gc; } public static synchronized java.util.Date getLastDayOfWeek( java.util.Date date ) { GregorianCalendar gc = ( GregorianCalendar ) Calendar.getInstance(); gc.setTime( date ); switch ( gc.get( Calendar.DAY_OF_WEEK ) ) { case ( Calendar.SUNDAY ): gc.add( Calendar.DATE, 6 ); break; case ( Calendar.MONDAY ): gc.add( Calendar.DATE, 5 ); break; case ( Calendar.TUESDAY ): gc.add( Calendar.DATE, 4 ); break; case ( Calendar.WEDNESDAY ): gc.add( Calendar.DATE, 3 ); break; case ( Calendar.THURSDAY ): gc.add( Calendar.DATE, 2 ); break; case ( Calendar.FRIDAY ): gc.add( Calendar.DATE, 1 ); break; case ( Calendar.SATURDAY ): gc.add( Calendar.DATE, 0 ); break; } return gc.getTime(); } public static synchronized java.util.Date getFirstDayOfWeek( java.util.Date date ) { GregorianCalendar gc = ( GregorianCalendar ) Calendar.getInstance(); gc.setTime( date ); switch ( gc.get( Calendar.DAY_OF_WEEK ) ) { case ( Calendar.SUNDAY ): gc.add( Calendar.DATE, 0 ); break; case ( Calendar.MONDAY ): gc.add( Calendar.DATE, -1 ); break; case ( Calendar.TUESDAY ): gc.add( Calendar.DATE, -2 ); break; case ( Calendar.WEDNESDAY ): gc.add( Calendar.DATE, -3 ); break; case ( Calendar.THURSDAY ): gc.add( Calendar.DATE, -4 ); break; case ( Calendar.FRIDAY ): gc.add( Calendar.DATE, -5 ); break; case ( Calendar.SATURDAY ): gc.add( Calendar.DATE, -6 ); break; } return gc.getTime(); } public static synchronized java.util.Calendar getFirstDayOfWeek( java.util.Calendar gc ) { switch ( gc.get( Calendar.DAY_OF_WEEK ) ) { case ( Calendar.SUNDAY ): gc.add( Calendar.DATE, 0 ); break; case ( Calendar.MONDAY ): gc.add( Calendar.DATE, -1 ); break; case ( Calendar.TUESDAY ): gc.add( Calendar.DATE, -2 ); break; case ( Calendar.WEDNESDAY ): gc.add( Calendar.DATE, -3 ); break; case ( Calendar.THURSDAY ): gc.add( Calendar.DATE, -4 ); break; case ( Calendar.FRIDAY ): gc.add( Calendar.DATE, -5 ); break; case ( Calendar.SATURDAY ): gc.add( Calendar.DATE, -6 ); break; } return gc; } public static synchronized java.util.Date getLastDayOfMonth( java.util.Date date ) { GregorianCalendar gc = ( GregorianCalendar ) Calendar.getInstance(); gc.setTime( date ); switch ( gc.get( Calendar.MONTH ) ) { case 0: gc.set( Calendar.DAY_OF_MONTH, 31 ); break; case 1: gc.set( Calendar.DAY_OF_MONTH, 28 ); break; case 2: gc.set( Calendar.DAY_OF_MONTH, 31 ); break; case 3: gc.set( Calendar.DAY_OF_MONTH, 30 ); break; case 4: gc.set( Calendar.DAY_OF_MONTH, 31 ); break; case 5: gc.set( Calendar.DAY_OF_MONTH, 30 ); break; case 6: gc.set( Calendar.DAY_OF_MONTH, 31 ); break; case 7: gc.set( Calendar.DAY_OF_MONTH, 31 ); break; case 8: gc.set( Calendar.DAY_OF_MONTH, 30 ); break; case 9: gc.set( Calendar.DAY_OF_MONTH, 31 ); break; case 10: gc.set( Calendar.DAY_OF_MONTH, 30 ); break; case 11: gc.set( Calendar.DAY_OF_MONTH, 31 ); break; } if ( ( gc.get( Calendar.MONTH ) == Calendar.FEBRUARY ) && ( isLeapYear( gc.get( Calendar.YEAR ) ) ) ) { gc.set( Calendar.DAY_OF_MONTH, 29 ); } return gc.getTime(); } public static synchronized java.util.Calendar getLastDayOfMonth( java.util.Calendar gc ) { switch ( gc.get( Calendar.MONTH ) ) { case 0: gc.set( Calendar.DAY_OF_MONTH, 31 ); break; case 1: gc.set( Calendar.DAY_OF_MONTH, 28 ); break; case 2: gc.set( Calendar.DAY_OF_MONTH, 31 ); break; case 3: gc.set( Calendar.DAY_OF_MONTH, 30 ); break; case 4: gc.set( Calendar.DAY_OF_MONTH, 31 ); break; case 5: gc.set( Calendar.DAY_OF_MONTH, 30 ); break; case 6: gc.set( Calendar.DAY_OF_MONTH, 31 ); break; case 7: gc.set( Calendar.DAY_OF_MONTH, 31 ); break; case 8: gc.set( Calendar.DAY_OF_MONTH, 30 ); break; case 9: gc.set( Calendar.DAY_OF_MONTH, 31 ); break; case 10: gc.set( Calendar.DAY_OF_MONTH, 30 ); break; case 11: gc.set( Calendar.DAY_OF_MONTH, 31 ); break; } //�ˬd�|�~ if ( ( gc.get( Calendar.MONTH ) == Calendar.FEBRUARY ) && ( isLeapYear( gc.get( Calendar.YEAR ) ) ) ) { gc.set( Calendar.DAY_OF_MONTH, 29 ); } return gc; } public static synchronized java.util.Date getFirstDayOfMonth( java.util.Date date ) { GregorianCalendar gc = ( GregorianCalendar ) Calendar.getInstance(); gc.setTime( date ); gc.set( Calendar.DAY_OF_MONTH, 1 ); return gc.getTime(); } public static synchronized java.util.Calendar getFirstDayOfMonth( java.util.Calendar gc ) { gc.set( Calendar.DAY_OF_MONTH, 1 ); return gc; } public static synchronized String toOraString( Date theDate, boolean hasTime ) { DateFormat theFormat; if ( hasTime ) { theFormat = getOraDateTimeFormat(); } else { theFormat = getOraDateFormat(); } return toString( theDate, theFormat ); } public static synchronized String toString( Date theDate, boolean hasTime ) { DateFormat theFormat; if ( hasTime ) { theFormat = getDateTimeFormat(); } else { theFormat = getDateFormat(); } return toString( theDate, theFormat ); } private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat( "MM/dd/yyyy" ); private static final SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat( "MM/dd/yyyy HH:mm" ); private static final SimpleDateFormat DATE_TIME_EXTENDED_FORMAT = new SimpleDateFormat( "MM/dd/yyyy HH:mm:ss" ); private static final SimpleDateFormat ORA_DATE_FORMAT = new SimpleDateFormat( "yyyyMMdd" ); private static final SimpleDateFormat ORA_DATE_TIME_FORMAT = new SimpleDateFormat( "yyyyMMddHHmm" ); private static final SimpleDateFormat ORA_DATE_TIME_EXTENDED_FORMAT = new SimpleDateFormat( "yyyyMMddHHmmss" ); public static synchronized DateFormat getDateFormat() { SimpleDateFormat theDateFormat = ( SimpleDateFormat ) DATE_FORMAT.clone(); theDateFormat.setLenient( false ); return theDateFormat; } public static synchronized DateFormat getDateTimeFormat() { SimpleDateFormat theDateTimeFormat = ( SimpleDateFormat ) DATE_TIME_FORMAT .clone(); theDateTimeFormat.setLenient( false ); return theDateTimeFormat; } public static synchronized DateFormat getOraDateFormat() { SimpleDateFormat theDateFormat = ( SimpleDateFormat ) ORA_DATE_FORMAT .clone(); theDateFormat.setLenient( false ); return theDateFormat; } public static synchronized DateFormat getOraDateTimeFormat() { SimpleDateFormat theDateTimeFormat = ( SimpleDateFormat ) ORA_DATE_TIME_FORMAT.clone(); theDateTimeFormat.setLenient( false ); return theDateTimeFormat; } public static synchronized String toString( Date theDate, DateFormat theDateFormat ) { if ( theDate == null ) return ""; return theDateFormat.format( theDate ); } } |
|
helian
2009-11-06
grails自带的日期时间输入有好几个参数,你改成文本框的话在data binding的时候就有问题,不喜欢那个的话用插件吧。比如http://www.grails.org/Tag+-+datePicker
|