0

I am new to Date functions. i have a date as a string '03/10/2014' - month/date/year. i need to convert this like '10-MAR-14' util date format. How can i do this please help me.

i used 'MM/dd/yyy' and 'dd-MMM-yyy' formats.

 public static Date convertStringToDate(String aMask, String strDate) throws ParseException { SimpleDateFormat df; Date date; df = new SimpleDateFormat(aMask); if (log.isDebugEnabled()) { log.debug("converting '" + strDate + "' to date with mask '" + aMask + "'"); } try { date = df.parse(strDate); } catch (ParseException pe) { //log.error("ParseException: " + pe); throw new ParseException(pe.getMessage(), pe.getErrorOffset()); } return (date); 

to do this. but gives me parse exception. Appreciate your help

but database column has the date-Month-year eg: 09-MAR-14. this does not give the result. i need to this conversion for hibernate criteria search. Database has results. but this conversion does not give the results.

4 Answers 4

4

You missed a y in the expression, use MM/dd/yyyy

Sign up to request clarification or add additional context in comments.

Comments

0

The following code will make a method and use it your logic

 import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class ex { public static void main(String[] args) { // TODO Auto-generated method stub SimpleDateFormat sdf1 = new SimpleDateFormat("dd-M-yyyy"); String dateInString = "15-08-2014"; Date date = null; try { date = sdf1.parse(dateInString); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy"); Calendar calendar = new GregorianCalendar(date.getYear(),date.getMonth(),date.getDate()); System.out.println("Date :" + sdf.format(calendar.getTime())); } 

} `

1 Comment

Thank you this is very helpful. I need to add one day to the date. how can i fix that issue
0
 SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy"); Date date = new Date(); String format = dateFormat.format(date); System.out.print("format is:"+format); 

Just follow this Simply

Comments

-2

It seems that the MSDN library has a format utility that can be downloaded to your project. See the link

1 Comment

Sorry for downvote. The question was belongs to Java so MSDN library could help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.