0

So the program is suppose to take a date in the format of "yyyy/dd/mm". After each date, there is text available to be entered. For example, "1776/7/4 First Independence Day". I wanted to use a String tokenizer and split the date at the slashes but then I am confused how to split it at the space after that. Once I split them, I can then compare the dates with other dates correct?

    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the first date: ");
    String firstDate = sc.nextLine();
    //System.out.print("Enter the second date: ");
    //String secondDate = sc.nextLine();
    //System.out.println();
    //StringTokenizer date = new StringTokenizer(firstDate, "/",false);
    //StringTokenizer space = new StringTokenizer(firstDate, " ",false);
    //if (firstDate.equals(secondDate))
    //{
     //   System.out.println (
       //         "There are 0 year(s), 0 month(s) and 0 day(s) between dates");
    //}
    int firstSpace = firstDate.indexOf(' ');

    String event = firstDate.substring(firstSpace + 1);    // "First Independence Day"

    String date = firstDate.substring(0, firstSpace);    // "1776/7/4"

    // split on '/'
    String dateParts[] = date.split("/");
    String dateParts1[] = date.split("/");
    String dateParts2[]= date.split("/");
    if(dateParts1[1].length() != 2)
        dateParts1[1] = "0" + dateParts1[1];

    // add 0 to front of day
    if(dateParts1[2].length() != 2)
        dateParts1[2] = "0" + dateParts1[2];

    // do the same for the second date
    if(dateParts2[1].length() != 2)
        dateParts2[1] = "0" + dateParts2[1];

    // convert array to string
    String date1 = String.join("/", dateParts1);
    String date2 = String.join("/", dateParts2);

    // set up the format that you have
    SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy/MM/dd");

    Date d1 = dateFormat.parse(date1);
    Date d2 = dateFormat.parse(date2);

    // equal dates
    if(d1.equals(d2)) {
        // There or 0 days, months and years between dates.
    }
John Doe
  • 23
  • 4

5 Answers5

0
String x = "yyyy/dd/mm";
String[] split = x.split("/");
for (String s : split) {
    System.out.println(s);
}


Output:
yyyy
dd
mm
rosstex
  • 773
  • 1
  • 9
  • 23
0

I think it would be a good idea to get the index of the first space character.

String text = "1776/7/4 First Independence Day";
                       ^

int firstSpace = text.indexOf(' ');

String event = text.substring(firstSpace + 1);    // "First Independence Day"

String date = text.substring(0, firstSpace);    // "1776/7/4"

// split on '/'
String dateParts[] = date.split("/");

This will ensure everything works even if the event name contains a /.

Edit.

To compare the dates, you will first need to ensure that months like April have a value of 04 and not 4. Say you have 2 arrays, dateParts1 and dateParts2.

// add 0 to front of month
if(dateParts1[1].length() != 2)
    dateParts1[1] = "0" + dateParts1[1];

// add 0 to front of day
if(dateParts1[2].length() != 2)
    dateParts1[2] = "0" + dateParts1[2];

// do the same for the second date;

// convert array to string
String date1 = String.join("/", dateParts1);
String date2 = String.join("/", dateParts2);

// set up the format that you have
SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy/MM/dd");

Date d1 = dateFormat.parse(date1);
Date d2 = dateFormat.parse(date2);

// equal dates
if(d1.equals(d2)) {
    // do something
}

if(date2.after(date1)) 
    return true;
else 
    return false;

Edit 2.

String firstDate = "1776/7/4 First Independence Day";

int firstSpace = firstDate.indexOf(' ');

String event = firstDate.substring(firstSpace + 1); // "First Independence Day"

String date = firstDate.substring(0, firstSpace); // "1776/7/4"

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

// add 0 to front of month
if (dateParts1[1].length() != 2)
    dateParts1[1] = "0" + dateParts1[1];

// add 0 to front of day
if (dateParts1[2].length() != 2)
    dateParts1[2] = "0" + dateParts1[2];

// do the same for the second date
if (dateParts2[1].length() != 2)
    dateParts2[1] = "0" + dateParts2[1];

// add 0 to front of day
if (dateParts2[2].length() != 2)
    dateParts2[2] = "0" + dateParts2[2];

// convert array to string
String date1 = String.join("/", dateParts1);
String date2 = String.join("/", dateParts2);

// set up the format that you have
java.text.SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");

java.util.Date d1 = dateFormat.parse(date1);
java.util.Date d2 = dateFormat.parse(date2);

if (d1.equals(d2))
    System.out.println("equal");
Debosmit Ray
  • 5,228
  • 2
  • 27
  • 43
  • @DesbosmitRay Thank you, so I print out the date from that and it gives me just the date. However, what is the logic behind comparing the year, month and day to another date? – John Doe Apr 24 '16 at 20:27
  • @JohnDoe First, compare by year. If same year, compare by month. If same year and month, compare by date. To compare values, you can use `[string1].compareTo([string2])`. Does that help? – Debosmit Ray Apr 24 '16 at 20:33
  • @DesbosmitRay Yes, I understand that part. I guess I am confused on how to just extract the year once it is split – John Doe Apr 24 '16 at 20:43
  • @JohnDoe I'll add it in. Your date portion is of the format `yyyy/mm/dd`, right? – Debosmit Ray Apr 24 '16 at 20:45
  • @DesbosmitRay I put up my current code that I have now from your help. I am getting a "String index out of bounds exception" error – John Doe Apr 24 '16 at 21:15
  • @JohnDoe on what line? note that you should get both dates to be equal now – Debosmit Ray Apr 24 '16 at 21:19
  • @DesbosmitRay Netbeans does not tell me what line but it crashes and says that as soon as I enter the first date – John Doe Apr 24 '16 at 21:23
  • I think making more edits will be completely out of the scope. If you have any further questions or extensions to this problem, you should post a separate question. – Debosmit Ray Apr 24 '16 at 21:25
  • @JohnDoe You did not copy this section correctly `// do the same for the second date` – Debosmit Ray Apr 24 '16 at 21:26
  • @DesbosmitRay Thank you, your code does work perfectly and I understand what is happening. Sorry about all of the questions, but my last one is how to take the input from the scanner and compare it instead of having the string of text already. When I switch it to my scanner variable, I get that error again – John Doe Apr 24 '16 at 21:28
  • @JohnDoe [Read this](http://stackoverflow.com/questions/12999899/getting-user-input-with-scanner) If there is a '\n' character at the end, you must remove it – Debosmit Ray Apr 24 '16 at 21:31
  • @JohnDoe It would be nice, if you could accept/upvote an answer if your issue is resolved. From [here](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work), "Accepting an answer is important as it both rewards posters for solving your problem and informs others that your issue is resolved." – Debosmit Ray Apr 24 '16 at 21:31
0

I'd recommend regular expression and DateFormat...

String input = "1776/07/04 First Independence Day";
Pattern pattern = Pattern.compile("(\\d+/\\d+/\\d+)\\s+(.*)");
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
    DateFormat format = new SimpleDateFormat("yyyy/M/d");
    Date date = format.parse(matcher.group(1));
    System.out.println(date + " " + matcher.group(2));
}
Adam
  • 35,919
  • 9
  • 100
  • 137
0

I don't quite understand your question. You can just take the first token which is date and save it in a new string and then you can either simply compare it using string.compareTo(another String); or you can take the year, month and day as integers using simply scanner.nextInt(): method and then compare them! Hope I helped.

  • This is not an answer. `I don't quite understand your question` The comment section is used to ask for clarifications. – Debosmit Ray Apr 24 '16 at 20:34
-1

just use splite like this

string data ;

string[] parts;

parts= data.splite(' ');

porat
  • 1
  • 1