Commit b02f784f by Patryk Czarnik

która godzina - pierwsze wersje

parent 48b106d9
package com.example.demo;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class DateTimeController {
@RequestMapping("/time1")
@ResponseBody
public LocalTime time1() {
return LocalTime.now();
}
@RequestMapping("/time2")
@ResponseBody
public String time2() {
return LocalTime.now().toString();
}
// https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/time/format/DateTimeFormatter.html#patterns
private static final DateTimeFormatter FORMAT_DATY = DateTimeFormatter
.ofPattern("EEEE, dd.MM.YYYY, 'godzina' HH:mm:ss", new Locale("pl", "PL"));
@RequestMapping(path="/time3", produces="text/plain")
@ResponseBody
public String time3() {
return LocalDateTime.now().format(FORMAT_DATY);
}
// Jak wysłać w odpowiedzi HTML?
// 1. Utworzyć bezpośrednio w kodzie Javy... - słabe
@RequestMapping(path = "/time4", produces = "text/html")
@ResponseBody
public String time4() {
LocalDateTime dt = LocalDateTime.now();
return String.format(
"<html><body><h1>Data i czas</h1>"
+ "<p>Teraz jest godzina <strong style='color:purple'>%s</strong></p>"
+ "<p>Dzisiejsza data: <strong style='color:blue'>%s</strong></p>"
+ "<p style='color: green'>%s</p>" + "</body></html>",
dt.toLocalTime(), dt.toLocalDate(), dt.format(FORMAT_DATY));
}
@RequestMapping("/time5")
public String time5() {
LocalDateTime dt = LocalDateTime.now();
return "show_time5.html";
}
}
......@@ -30,8 +30,8 @@ public class HelloController {
// która wypisuje biężacy czas
@RequestMapping("/ktora-godzina")
@ResponseBody
public LocalTime ktoraGodzina() {
return LocalTime.now();
public String ktoraGodzina() {
return "Jest godzina " + LocalTime.now();
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Która godzina 5</title>
</head>
<body>
<h1>Która godzina 5</h1>
<p>Zaraz powiem, która jest godzina...</p>
</body>
</html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment