-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
56 lines (51 loc) · 2.34 KB
/
Solution.java
File metadata and controls
56 lines (51 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.javarush.task.task40.task4008;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.util.Calendar;
/*
Работа с Java 8 DateTime API
*/
public class Solution {
public static void main(String[] args) {
printDate("21.4.2014 15:56:45");
System.out.println();
printDate("21.4.2014");
System.out.println();
printDate("17:33:40");
}
public static void printDate(String date) {
boolean printDate = true;
boolean printTime = true;
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("d.M.y H:m:s");
if (date.contains(".") && !date.contains(":")) {
dateFormat = DateTimeFormatter.ofPattern("d.M.y");
printTime = false;
}
if (date.contains(":") && !date.contains(".")) {
dateFormat = DateTimeFormatter.ofPattern("H:m:s");
printDate = false;
}
if (printDate) {
LocalDate localDate = LocalDate.parse(date, dateFormat);
System.out.println("День: " + localDate.getDayOfMonth());
System.out.println("День недели: " + localDate.getDayOfWeek().getValue());
System.out.println("День месяца: " + localDate.getDayOfMonth());
System.out.println("День года: " + localDate.getDayOfYear());
System.out.println("Неделя месяца: " + localDate.format(DateTimeFormatter.ofPattern("W")));
System.out.println("Неделя года: " + localDate.format(DateTimeFormatter.ofPattern("w")));
System.out.println("Месяц: " + localDate.getMonth().getValue());
System.out.println("Год: " + localDate.getYear());
}
if (printTime) {
LocalTime localTime = LocalTime.parse(date, dateFormat);
System.out.println("AM или PM: " + localTime.format(DateTimeFormatter.ofPattern("a")));
System.out.println("Часы: " + localTime.format(DateTimeFormatter.ofPattern("K")));
System.out.println("Часы дня: " + localTime.getHour());
System.out.println("Минуты: " + localTime.getMinute());
System.out.println("Секунды: " + localTime.getSecond());
}
}
}