Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* A beer time is after 1:00 PM and before 3:00 AM.
- * Write a program that enters a time in format “h:mm tt” (an hour in range [01...12],
- * a minute in range [00…59] and AM / PM designator) and prints “beer time”
- * or “non-beer time” according to the definition above or “invalid time”
- * if the time cannot be parsed. Note that you may need to learn how to parse dates and times. */
- import java.time.LocalTime;
- import java.time.format.DateTimeFormatter;
- import java.util.Locale;
- import java.util.Scanner;
- public class BeerTime {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Locale.setDefault(Locale.ROOT);
- Scanner scan = new Scanner(System.in);
- System.out.print("Enter a time to check, exactly in that format [h:mm AM/PM] : ");
- String timeStr = scan.nextLine();
- scan.close();
- String timeFormat = "h:mm a";
- try {
- LocalTime timeToCheck = LocalTime.parse(timeStr,
- DateTimeFormatter.ofPattern(timeFormat, Locale.ROOT));
- int hour = timeToCheck.getHour();
- if (hour >= 3 && hour <= 12) {
- System.out.println("Now is non-beer time!");
- } else {
- System.out.println("Now is Beer time!");
- }
- } catch (Exception e) {
- // TODO: handle exception
- System.out.println("Error! - Invalid time!!!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement