1

In my program I need to loop through a variety of dates. I am writing this program in java, and have a bit of experience with readers, but I do not know which reader would complete this task the best, or if another class would work better. The dates would be input into a text file in the format as follows:

1/1/2013 to 1/7/2013
1/8/2013 to 1/15/2013

Or something of this manner. I would need to break each range of dates into 6 local variables for the loop, then change them for the next loop. The variables would be coded for example:

private static String startingMonth = "1";
  private static String startingDay = "1";
  private static String startingYear = "2013";
  private static String endingMonth = "1";
  private static String endingDay = "7";
  private static String endingYear = "2013";

I imagine this could be done creating several delimiters to look for, but I do not know that this would be the easiest way. I have been looking at this post for help, but cant seem to find a relevant answer. What would be the best way to go about this?

Community
  • 1
  • 1
Ctech45
  • 496
  • 9
  • 17

2 Answers2

0
    String str = "1/1/2013 to 1/7/2013";
    Pattern pattern = Pattern.compile("(\\d+/\\d+/\\d+)");
    Matcher matcher = pattern.matcher(str);
    matcher.find();
    String startDate = matcher.group();
    matcher.find();
    String endDate = matcher.group();
    String[] start = startDate.split("/");
    System.out.println(start[0] + "-" + start[1] + "-" + start[2]);
    String[] end = endDate.split("/");
    System.out.println(end[0] + "-" + end[1] + "-" + end[2]);
    ...

OUTPUT

1-1-2013
1-7-2013
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • Essentially, instead of creating one large string of the whole date, I could reference each part of "start" to get the month, day, year? Also, Could I just use a reader to read in each line of the text file? – Ctech45 Nov 04 '13 at 22:24
  • @Connor In the code that I posted, `start[0]` holds "1" from `1/1/2013`, `start[1]` holds the other "1" and `start[2]` holds `2013`. So you can use a string array or string parameters to capture these values. – Nir Alfasi Nov 04 '13 at 22:28
  • As for your second question, yes, you can read each line using a `Scanner` or a `BufferedReader` - and then apply the parsing part that uses the regex. – Nir Alfasi Nov 04 '13 at 22:29
0

There are several options.

You could use the scanner, and set the delimiter to include the slash. If you want the values as ints and not string, just use sc.nextInt()

Scanner sc = new Scanner(input).useDelimiter("\\s*|/");
// You can skip the loop to just read a single line.
while(sc.hasNext()) {
  startingMonth = sc.next();
  startingDay = sc.next();
  startingYear = sc.next();
  // skip "to"
  sc.next()
  endingMonth = sc.next();
  endingDay = sc.next();
  endingYear = sc.next();
}

You can use regex, as alfasin suggest, but this case is rather simple so you can just match the first and last space.

String str = "1/1/2013 to 1/7/2013";
String startDate = str.substring(0,str.indexOf(" "));
String endDate = str.substring(str.lastIndexOf(" ")+1);¨
// The rest is the same:
String[] start = startDate.split("/");
System.out.println(start[0] + "-" + start[1] + "-" + start[2]);
String[] end = endDate.split("/");
System.out.println(end[0] + "-" + end[1] + "-" + end[2]);
Tobber
  • 7,211
  • 8
  • 33
  • 56