-2

I am a Java student in high school and I would like to know how to find the position of a certain character of group of characters no matter the length..

For example, if I am asking a user for a input via:

java.util.Scanner;

and they were asked to enter a date (mm/dd/yy) and usually I would do:

date.substring(0,2);

to get months but I learned this phrase my CS teacher "Users are dumb" so If they entered the date format in 1/3/2019 then the position of the month is different so how can i use something that looks for the month, day, or year despite the incorrect use of the date format?

  • 1
    It is really hard if you don't know the position, you might need to write your own logic for that – Ryuzaki L Sep 20 '19 at 00:31
  • Possible duplicate of [How to parse date string to Date?](https://stackoverflow.com/questions/4496359/how-to-parse-date-string-to-date) – George Z. Sep 20 '19 at 00:34
  • 1
    or search for `Regular Expressions`. – George Z. Sep 20 '19 at 00:34
  • Deadpool is correct, especially when you are dealing with ambiguous value on the string that represent date (e.g. 12/12/11), especially month and date, and worse if they decide to shorten the year into two digits. – Bagus Tesa Sep 20 '19 at 00:36

1 Answers1

1

In terms of date you can do this using split() like so:

String[] dateSplit = date.split("/");

This will give you the date in three parts:

dateSplit[0] = //mm
dateSplit[1] = //dd
dateSplit[2] = //yyyy
DrRoach
  • 1,320
  • 9
  • 16