1

I'm using Java's default TimeZone and Calendar classes to try and get the time of different areas, however when I try to use it, it doesn't take into account any +0x:00. For example when I enter "Europe/England" it returns me 1:30, when it's actually 2:30 since right now England is using GMT+1, not GMT.

String timeZone = raw.split(" ")[1];
Calendar calendar = new GregorianCalendar();
TimeZone tz;

try {
    tz = TimeZone.getTimeZone(timeZone);
    calendar.setTimeZone(tz);
} catch (Exception e) {
    event.getChannel().sendMessage("Couldn't find time-zone for: " + timeZone +
        ".\n*Usage: !time <continent/city>*\n*You can find TZ names here: " +
        "https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List*").queue();
    return;
}

long hours = calendar.get(Calendar.HOUR_OF_DAY);
String minutes = String.valueOf(calendar.get(Calendar.MINUTE));
if (minutes.length() == 1)
    minutes = "0" + minutes;
User author = event.getAuthor();
event.getChannel().sendMessage(author.getAsMention() + " The current time is: **" + hours + ":" + minutes + "** [" + tz.getDisplayName() + "]").queue();
Bashir
  • 2,057
  • 5
  • 19
  • 44

1 Answers1

2

I suggest you switch to modern date/time API instead of using the outdated date/time API.

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;

public class Main {
    public static void main(String[] args) {
        ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("Europe/London"));
        System.out.println(zdt);
        System.out.println(zdt.getHour());
        System.out.println(zdt.getMinute());

        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                .appendLiteral("Hour: ")
                .appendValue(ChronoField.HOUR_OF_DAY)
                .appendLiteral(", Minute: ")
                .appendValue(ChronoField.MINUTE_OF_HOUR)
                .toFormatter();
        System.out.println(formatter.format(zdt));
    }
}

Output:

2020-06-11T14:54:34.081332+01:00[Europe/London]
14
54
Hour: 14, Minute: 54
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • 1
    The time is now correct however now I can't input things like 'England' anymore. – Alex Hammond Jun 11 '20 at 14:01
  • 2
    @AlexHammond `Europe/England` is not a valid timezone - I'm surprised the old APIs accept it. As your exception-catching comment says, "Usage: !time ", and you'll notice it doesn't appear in the list linked there either. "England" isn't a city; "Europe/London" is the correct timezone name for the UK, and the one you should be using. – Andrzej Doyle Jun 11 '20 at 14:04
  • 1
    Thanks, Andrzej Doyle. @AlexHammond - You can print [java.util.TimeZone#getAvailableIDs()](https://docs.oracle.com/javase/7/docs/api/java/util/TimeZone.html#getAvailableIDs%28%29) to verify that there is no timezone such as `Europe/England`. – Arvind Kumar Avinash Jun 11 '20 at 14:08
  • 2
    @AlexHammond This is why you should not use the old API. It silently rejects your invalid timezone name and defaults to GMT - see the javadoc entry [here](https://docs.oracle.com/javase/7/docs/api/java/util/TimeZone.html#getTimeZone(java.lang.String)) - "_the specified TimeZone, or the GMT zone if the given ID cannot be understood._" – andrewJames Jun 11 '20 at 14:09
  • 1
    Wikipedia has a [list of time zones](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones). – Basil Bourque Jun 11 '20 at 17:31