Ошибка при компиляции Java ZoneId.of(ZoneId.java:411)
Выдает такие ошибки:
Exception in thread "main" java.time.zone.ZoneRulesException: Unknown time-zone ID: Europe/France
at java.time.zone.ZoneRulesProvider.getProvider(ZoneRulesProvider.java:272)
at java.time.zone.ZoneRulesProvider.getRules(ZoneRulesProvider.java:227)
at java.time.ZoneRegion.ofId(ZoneRegion.java:120)
at java.time.ZoneId.of(ZoneId.java:411)
at java.time.ZoneId.of(ZoneId.java:359)
at Clock.clock(Prog.java:21)
at Main.main(Main.java:15)
Вот код
import java.io.IOException;
import java.util.Scanner;
public class Main
{
static Clock clock;
public static void main(String[] args) throws IOException
{
clock = new Clock();
Scanner in = new Scanner(System.in);
System.out.println("Enter the locale: ");
clock.location = in.next();
System.out.println("Enter delay: ");
clock.delay = in.nextInt();
clock.clock();
}
}
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
class Clock
{
public int delay;
public String location;
public void clock() throws IOException
{
while (true)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
LocalDateTime localdatetime = LocalDateTime.now(ZoneId.of(location));
String Date_Time = "\n" + location + " " +
localdatetime.format(formatter) + "\n" +
"In the binary system : " + Integer.toBinaryString(localdatetime.getDayOfMonth()) +
" " + Integer.toBinaryString(localdatetime.getMonthValue()) +
" " + Integer.toBinaryString(localdatetime.getYear()) +
" " + Integer.toBinaryString(localdatetime.getHour()) +
" " + Integer.toBinaryString(localdatetime.getMinute()) +
" " + Integer.toBinaryString(localdatetime.getSecond()) + "\n" +
"In the hexadecimal system : " + Integer.toHexString(localdatetime.getDayOfMonth()) +
" " + Integer.toHexString(localdatetime.getMonthValue()) +
" " + Integer.toHexString(localdatetime.getYear()) +
" " + Integer.toHexString(localdatetime.getHour()) +
" " + Integer.toHexString(localdatetime.getMinute()) +
" " + Integer.toHexString(localdatetime.getSecond());
System.out.println(Date_Time);
try (FileWriter writer = new FileWriter("src/com/lab/Results.txt", true))
{
writer.write(Date_Time);
writer.write('\n');
}
catch (IOException ex)
{
System.out.println(ex.getMessage());
}
try
{
Thread.sleep(delay * 1000);
if (System.in.available() !=0)
{
break;
}
}
catch (InterruptedException e)
{ }
}
}
}
В чем может быть проблема?
Ответы (1 шт):
Автор решения: Barmaley
→ Ссылка
- Это не ошибка компиляции, а run-time ошибка во время выполнения
- Это штатное поведение
ZoneId.of(), который согласно документации может выкидывать 2 исключения: DateTimeException - if the zone ID has an invalid format и ZoneRulesException - if the zone ID is a region ID that cannot be found
Вам нужно:
- Корректно обрабатывать исключение, через
catch(ZoneRulesException ex) - Подавать на вход корректную тайм-зону (в комментариях указали варианты)