import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Date today = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String dateToStr = format.format(today);
System.out.println(dateToStr);
System.out.println("Input time:");
Scanner scanner = new Scanner(System.in);
String inputTime = scanner.nextLine();
// String time = "13:10";
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); // or "hh:mm" for 12 hour format
Date date = sdf.parse(inputTime);
int hour = date.getHours(); // int
int min = date.getMinutes(); // int
System.out.println("Time input: " + hour + ":" + min);
}
}
No comments:
Post a Comment