0

I have a class that parses ZonedDateTime objects using.split() to get rid of all the extra information I don't want.

My Question: Is there a way to use square brackets as delimiters that I am missing, OR how do I get the time zone ([US/Mountain]) by itself without using square brackets as delimiters?

I want the String timeZone to look like "US/Mountian" or "[US/Mountian]

What I've Tried: Ive tried wholeThing.split("[[-T:.]]?) and wholeThing.split("[%[-T:.%]]") but those both give me 00[US/Mountain]

I've also tried wholeThing.split("[\\[-T:.\\]]) and wholeThing.split("[\[-T:.\]") but those just give me errors.

(part of) My Code:

 //We start out with something like   2016-09-28T17:38:38.990-06:00[US/Mountain]

    String[] whatTimeIsIt = wholeThing.split("[[-T:.]]"); //wholeThing is a TimeDateZone object converted to a String
    String year = whatTimeIsIt[0];
    String month = setMonth(whatTimeIsIt[1]);
    String day = whatTimeIsIt[2];
    String hour = setHour(whatTimeIsIt[3]);
    String minute = whatTimeIsIt[4];
    String second = setAmPm(whatTimeIsIt[5],whatTimeIsIt[3]);
    String timeZone = whatTimeIsIt[8];
StarSweeper
  • 397
  • 2
  • 4
  • 28
  • Why don't you parse a ZonedDateTime string like that using .... you know .... `ZonedDateTime.parse()`? --- Anyway, your question is unclear on what output you want. What do you want the `timeZone` string to be? `-06:00`? `US/Mountain`? Please clarify. --- But, it would be better to *use* the regex, rather than trying to use `split()`, i.e. using `Pattern` and `Matcher`, so you can get the appropriate pieces using the `group(x)` method. If you don't know regex, now would be the time to learn. – Andreas Oct 05 '16 at 03:28
  • Just to clarify, you want to be able to just get the timezone from the String? – DarkHorse Oct 05 '16 at 03:28
  • Edited to clarify – StarSweeper Oct 05 '16 at 03:31
  • @andreas because I wanted practice doing it myself and because ZonedDateTime.parse() doesn't give me exactly what I want. My class does, except for this part that I'm stuck on. – StarSweeper Oct 05 '16 at 03:36
  • *Curious:* In what way doesn't `ZonedDateTime.parse()` not give you what you want? – Andreas Oct 05 '16 at 03:54
  • @Andreas I couldn't find many examples and the ones I did find didn't look any different than what I already had. Either way I like it better this way. – StarSweeper Oct 05 '16 at 04:13

2 Answers2

1

Using split() is the right idea.

String[] timeZoneTemp = wholeThing.split("\\[");
String timeZone = timeZoneTemp[1].substring(0, timeZoneTemp[1].length() - 1);
DarkHorse
  • 963
  • 1
  • 10
  • 28
1

If you want to parse the string yourself, use a regular expression to extract the values.

Don't use a regex to find characters to split on, which is what split() does.

Instead, use a regex with capture groups, compile it using Pattern.compile(), obtain a Matcher on your input text using matcher(), and check it using matches().

If it matches you can get the captured groups using group().

Example regex:

(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}).(\d+)[-+]\d{2}:\d{2}\[([^\]]+)\]

In a Java string, you have to escape the \, so here is code showing how it works:

String input = "2016-09-28T17:38:38.990-06:00[US/Mountain]";

String regex = "(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2}).(\\d+)[-+]\\d{2}:\\d{2}\\[([^\\]]+)\\]";
Matcher m = Pattern.compile(regex).matcher(input);
if (m.matches()) {
    System.out.println("Year    : " + m.group(1));
    System.out.println("Month   : " + m.group(2));
    System.out.println("Day     : " + m.group(3));
    System.out.println("Hour    : " + m.group(4));
    System.out.println("Minute  : " + m.group(5));
    System.out.println("Second  : " + m.group(6));
    System.out.println("Fraction: " + m.group(7));
    System.out.println("TimeZone: " + m.group(8));
} else {
    System.out.println("** BAD INPUT **");
}

Output

Year    : 2016
Month   : 09
Day     : 28
Hour    : 17
Minute  : 38
Second  : 38
Fraction: 990
TimeZone: US/Mountain

UPDATED

You can of course get all the same values using ZonedDateTime.parse(), which will also ensure that the date is valid, something none of the other solutions will do.

String input = "2016-09-28T17:38:38.990-06:00[US/Mountain]";

ZonedDateTime zdt = ZonedDateTime.parse(input);
System.out.println("Year    : " + zdt.getYear());
System.out.println("Month   : " + zdt.getMonthValue());
System.out.println("Day     : " + zdt.getDayOfMonth());
System.out.println("Hour    : " + zdt.getHour());
System.out.println("Minute  : " + zdt.getMinute());
System.out.println("Second  : " + zdt.getSecond());
System.out.println("Milli   : " + zdt.getNano() / 1000000);
System.out.println("TimeZone: " + zdt.getZone());

Output

Year    : 2016
Month   : 9
Day     : 28
Hour    : 17
Minute  : 38
Second  : 38
Milli   : 990
TimeZone: US/Mountain
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Thanks for the ZonedDateTime.parse() example, I couldn't find a decent one. – StarSweeper Oct 05 '16 at 04:31
  • There probably were no examples of this, because you'd find the information easily by looking at the [documentation](https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html#method.summary) of `ZonedDateTime`. – Andreas Oct 05 '16 at 15:46