java - Getting date in format dd-MMM-yyyy -
i have below method shown below in date coming of string:
public static java.sql.date getsimpledate11(string datestring) { if (datestring == null) { return null; } java.util.date date = null; java.sql.date sqldate =null ; try { dateformat df = new simpledateformat("yyyy-mm-dd"); df.setlenient(false); date = df.parse(datestring); sqldate = new java.sql.date(date.gettime()); } catch (parseexception pe) { throw new runtimeexception("the date entered invalid or has incorrect format"+ datestring); } return sqldate;
now want change format: want store date in format dd-mmm-yyyy.
what should do?
now want change format want store date in format
dd-mmm-yyyy
you don't need explicit conversion requested date format dd-mmm-yyyy
.
dates not directly concerned date formats. sql driver class convert proper database specific format before inserting date field of database table.
using mysql driver:
// statement cause sql date '2014-03-21' new java.sql.date( new java.until.date().gettime() );
in of databases default format yyyy-mm-dd
.
example (mysql):
mysql> show variables 'date_format'; +---------------+----------+ | variable_name | value | +---------------+----------+ | date_format | %y-%m-%d | +---------------+----------+ 1 row in set (0.00 sec) mysql> select curdate(); +------------+ | curdate() | +------------+ | 2014-03-21 | +------------+ 1 row in set (0.05 sec)
detailed example:
public class simpledateformat_example { public static void main(string[] args) throws exception { string dateinputpattern = "yyyy-mm-dd"; // numeric 2 digit month string datetargetpattern = "yyyy-mmm-dd"; // 3 char month name string datestring = "2014-03-20"; patterntest( dateinputpattern, datestring, datetargetpattern ); system.out.println(); // day of month first , 2 digit month dateinputpattern = "yyyy-dd-mm"; datestring = "2014-21-03"; datetargetpattern = "yyyy-mmmm-dd, eeee"; // full month name patterntest( dateinputpattern, datestring, datetargetpattern ); } // psvm( ... ) public static void patterntest( string dateinputpattern, string datestring, string datetargetpattern ) throws exception { java.text.simpledateformat sdf = new java.text.simpledateformat( dateinputpattern ); java.util.date date = sdf.parse( datestring ); system.out.println( "date pattern: " + dateinputpattern ); system.out.println( "date string : " + datestring ); system.out.println( "date value : " + date ); sdf.applypattern( datetargetpattern ); system.out.println( "target pattern: " + datetargetpattern ); system.out.println( "pattern based date value: " + sdf.format(date) ); java.sql.date sqldate = new java.sql.date( date.gettime() ); system.out.println( "but, sql date: " + sqldate ); } // patterntest( s, s, s ) } // end of class simpledateformat_example
if run above program seeing following results.
results:
date pattern: yyyy-mm-dd date string : 2014-03-20 date value : thu mar 20 00:00:00 ist 2014 target pattern: yyyy-mmm-dd pattern formatted date value: 2014-mar-20 but, sql date: 2014-03-20 date pattern: yyyy-dd-mm date string : 2014-21-03 date value : fri mar 21 00:00:00 ist 2014 target pattern: yyyy-mmmm-dd, eeee pattern formatted date value: 2014-march-21, friday but, sql date: 2014-03-21
have close @ sql date, has same pattern in outputs. same matching result of select curdate()
pattern, shown in top of answer.
conclusion:
though set , apply specific pattern, in scripting language, date type database field, stored in default
pattern defined database date types.
hence, converting pattern yyyy-mm-dd
yyyy-mmm-dd
, sending database not affect , change anything.
Comments
Post a Comment