2

I have to add into a Database a string in format "MM-YY" which represents expiration date from a Credit Card , where MM is the month and YY are the last two digits of the Year , how do I check if a String is in this this specific format in Java ?

  • have look at: https://stackoverflow.com/questions/20231539/java-check-the-date-format-of-current-string-is-according-to-required-format-or – rieckpil Sep 04 '18 at 16:20
  • @rieckpil This Question is not an exact duplicate of [that Question](https://stackoverflow.com/questions/20231539/java-check-the-date-format-of-current-string-is-according-to-required-format-or). That one was about year-month-day while this one is about year-month. Different solution, using [`YearMonth`](https://docs.oracle.com/javase/10/docs/api/java/time/YearMonth.html) class here. – Basil Bourque Sep 04 '18 at 22:32
  • Similar: [*Comparing dates in MMYY format*](https://stackoverflow.com/q/38134680/642706) – Basil Bourque Sep 04 '18 at 22:34
  • @BasilBourque, I didn't mark this question as a duplicate. Is just posted a possible way to support finding the solution for himself ;) – rieckpil Sep 05 '18 at 05:54
  • thanks everyone , I resolved the Issue – Lør_Zømbie Sep 05 '18 at 16:20

3 Answers3

0

Use a regular expression:

String myString = "12-99";

if( myString.matches("((0[1-9])|(1[0-2]))-\\d{2}") ){
   System.out.println("Correct format");
}
else{
  System.out.println("Incorrect format");
}
krokodilko
  • 35,300
  • 7
  • 55
  • 79
0

Try Using below regex

String str = "10-18";
if (str.matches("([0-9]{2})-([0-9]{2})"))
 System.out.println("Correct");
else 
System.out.println("Incorrect");
Pallav Kabra
  • 418
  • 3
  • 7
0

java.time.YearMonth

Parse as a YearMonth. Trap for exception.

String input = "99-18" ;  // Valid: "01-18"  Invalid: "99-18"
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM-uu" ) ;

YearMonth ym = null ;
try {
    ym = YearMonth.parse( input , f ) ;
} catch ( DateTimeParseException e ) {
    System.out.println( "Invalid input: " + input ) ;
}

System.out.println( "ym.toString(): " + ym ) ;

See this code run live on IdeOne.com.

Invalid input: 99-18

ym.toString(): null


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154