1

I'm using a scanner that gets details from a UK driving licence. However it returns the date as a string and most current uk driving licences are dd-mm-yy (16-10-10). Is there a way I can make it dd-mm-yyyy but with the right two numbers in front of it i.e. 16-10-2010?

Thanks

BilalMH
  • 175
  • 1
  • 21
  • You can use the `DateFormatter` from the standard library. Besides that, what about licenses before 2000? – Murat Karagöz Jul 04 '18 at 11:21
  • Would it catch that as well? Obviously I don't want like `2093` – BilalMH Jul 04 '18 at 11:24
  • Pretty sure it's going to be written as 1993. – Murat Karagöz Jul 04 '18 at 11:25
  • you can use SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); – Karan Chunara Jul 04 '18 at 11:30
  • @KaranChunara Consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Jul 04 '18 at 16:42

1 Answers1

0
    String strDate = "16-10-10";
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yy");

    try {
        Date date = dateFormat.parse(strDate);
        Format formatter = new SimpleDateFormat("MM-dd-yyyy");
        String s = formatter.format(date);
        System.out.println(s);

    } catch (ParseException e) {
        e.printStackTrace();
    }
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Jul 04 '18 at 16:23