1

In the following code input:

Enter Date: 3/2/2011

Output:

Entered Date is February 3, 2011
Entered Month is 02

Problem is , when i input this date 3/14/2012, the date format function automatically changes month to 12+2(February). If I put 13/15/2011, it will change month to 3(12+3).

It should give an error on 14 that "invalid month"

package lesson4;

import java.util.*;

import java.text.*;
public class ConvertDate {
static String Month;
static String fulldate;
static int month;
static  int[] montharray={1,2,3,4,5,6,7,8,9,10,11,12};
public static void main(String[] args){


Scanner sc = new Scanner(System.in); 
System.out.print("Enter Date: "); 
String ind = sc.nextLine(); 
//Date now = new Date();
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat f = new SimpleDateFormat("dd");
SimpleDateFormat m = new SimpleDateFormat("MM");

Date d = null;
    Date e=null;
Date g=null;


try {
d=df.parse(ind);
e=df.parse(ind);
g=df.parse(ind);
DateFormat df3 = DateFormat.getDateInstance(DateFormat.LONG);


 fulldate = df3.format(d);
 Month=m.format(g);
month =Integer.parseInt(Month);

String date  =f.format(e);
 } catch (ParseException e1) {
// TODO Auto-generated catch block



e1.printStackTrace();
}





System.out.println("The entered date is: " + fulldate);
System.out.println("The entered month is: " + Month);

}
}
3
  • bro can you give example..i'll be greatful Commented Aug 2, 2013 at 7:26
  • @Nizil. No it won't be. DateFormat is the correct way to do this. Commented Aug 2, 2013 at 7:28
  • @RohitJain I agree, DateFormat is so much better than string parsing, but forgot the setLenien(bool) and didn't see another solution before you post your answer ;) Commented Aug 2, 2013 at 7:30

4 Answers 4

4

For each of your DateFormat instance, you need to invoke setLenient with false argument:

DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
df.setLenient(false);
DateFormat f = new SimpleDateFormat("dd");
f.setLenient(false);
DateFormat m = new SimpleDateFormat("MM");
m.setLenient(false);

From DateFormat#setLenient(boolean) documentation:

With lenient parsing, the parser may use heuristics to interpret inputs that do not precisely match this object's format. With strict parsing, inputs must match this object's format.

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

14 Comments

great Rohit..but can i want to check the month from array..i am restricted to use arrays if i enter 14 in month then it should display invalid month,....Your code is only catching exception
@JamesBlent. You can catch the exception, and display your own message. You can search for some good Exception Handling tutorial.
Yes i have cached exception that "Invalid Date"..but how can i give error using array..i have declared array in the code...please help
@JamesBlent. What do you mean by give error using array? Can you be more specific?
yes ...i want that if i enter date 14/14/2011..It should give an error that invalid month...I want that i check input value of month(14) convert it to int and compare it to my declared array..
|
2

Reference these formats Java Date Format Docs:

Formats for datetime

DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

you are expecting Month at second place while input is placing it on first.

try:

DateFormat df = new SimpleDateFormat("MM/dd/yyyy");

Comments

1

tl;dr

Formatting pattern must match your actual inputs.

java.time.LocalDate.parse ( "3/14/2012" , DateTimeFormatter.ofPattern ( "M/d/uuuu" )  )

java.time

In modern Java (8+), use only the java.time classes for date-time work. Never use Calendar nor SimpleDateFormat.

Your formatting pattern failed to match your input. Get your month number and day-of-month codes in the right place.

Another problem in your code: Use single d & M if not padding with leading zero for single-digit values. If padding, use double dd & MM.

DateTimeFormatter f = DateTimeFormatter.ofPattern ( "M/d/uuuu" ) ;
LocalDate ld = LocalDate.parse ( "3/14/2012" , f ) ;

To detect invalid inputs, trap for DateTimeParseException.

Comments

0

Have you tried using "setLenient(false)" on your DateFormat to force the DateFormat to be strict about the parsed input? I haven't tried it yet, but stumbled across this feature recently.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.