Skip to content

Commit 5fce2a2

Browse files
committed
Format code for Thymeleaf application
1 parent 021133c commit 5fce2a2

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

datetime-thymeleaf/src/main/java/com/example/lulunac27a/datetimethymeleaf/DateTimeController.java

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515

1616
@Controller
1717
public class DateTimeController {
18-
@GetMapping("/")
18+
@GetMapping("/") // get request from home page
1919
public String dateTimeForm(Model model) {
20-
DateTime dateTime = new DateTime();
20+
DateTime dateTime = new DateTime();// create date and time object
2121
model.addAttribute("enteredDateTime", dateTime);
22-
LocalDateTime currentDateTime = LocalDateTime.now();
22+
LocalDateTime currentDateTime = LocalDateTime.now();// set current date and time to now
2323
dateTime.setYear(currentDateTime.getYear());
2424
dateTime.setMonth(currentDateTime.getMonthValue());
2525
dateTime.setDay(currentDateTime.getDayOfMonth());
@@ -31,10 +31,18 @@ public String dateTimeForm(Model model) {
3131
dateTime.setNanosecond(currentDateTime.getNano() % 1000);
3232
LocalDateTime dateTimeValues = LocalDateTime.of(dateTime.getYear(), dateTime.getMonth(), dateTime.getDay(),
3333
dateTime.getHour(), dateTime.getMinute(), dateTime.getSecond(),
34-
dateTime.getMillisecond() * 1000000 + dateTime.getMicrosecond() * 1000 + dateTime.getNanosecond());
34+
dateTime.getMillisecond() * 1000000 + dateTime.getMicrosecond() * 1000 + dateTime.getNanosecond());// set
35+
// date
36+
// and
37+
// time
38+
// values
3539
model.addAttribute("currentDateTime", dateTimeValues);
3640
Instant currentUtcDateTime = Instant.now();
37-
LocalDateTime currentUtcDateTimeNow = LocalDateTime.ofInstant(currentUtcDateTime, ZoneId.of("UTC"));
41+
LocalDateTime currentUtcDateTimeNow = LocalDateTime.ofInstant(currentUtcDateTime, ZoneId.of("UTC"));// set
42+
// current
43+
// UTC date
44+
// and time
45+
// to now
3846
DateTime utcDateTimeValues = new DateTime();
3947
utcDateTimeValues.setYear(currentUtcDateTimeNow.getYear());
4048
utcDateTimeValues.setMonth(currentUtcDateTimeNow.getMonthValue());
@@ -50,22 +58,33 @@ public String dateTimeForm(Model model) {
5058
utcDateTimeValues.getSecond(), utcDateTimeValues.getMillisecond() * 1000000
5159
+ utcDateTimeValues.getMicrosecond() * 1000 + utcDateTimeValues.getNanosecond());
5260
model.addAttribute("currentUtcDateTime", utcDateTimeValuesNow);
53-
return "index";
61+
return "index";// return index page
5462
}
5563

56-
@PostMapping("submit-form")
57-
public String formatDateTime(@ModelAttribute("enteredDateTime") DateTime dateTime, Model model) {
64+
@PostMapping("submit-form") // POST request from submiting the form
65+
public String formatDateTime(@ModelAttribute("enteredDateTime") DateTime dateTime, Model model) {// format date and
66+
// time using date
67+
// and time format
68+
// patterns
5869
model.addAttribute("enteredDateTime", dateTime);
5970
LocalDateTime enteredDateTime = LocalDateTime.of(dateTime.getYear(), dateTime.getMonth(), dateTime.getDay(),
6071
dateTime.getHour(), dateTime.getMinute(), dateTime.getSecond(),
61-
dateTime.getMillisecond() * 1000000 + dateTime.getMicrosecond() * 1000 + dateTime.getNanosecond());
62-
model.addAttribute("enteredDateTime", dateTime);
63-
model.addAttribute("dateTimeOutput", enteredDateTime);
64-
return "result";
72+
dateTime.getMillisecond() * 1000000 + dateTime.getMicrosecond() * 1000 + dateTime.getNanosecond());// set
73+
// entered
74+
// date
75+
// and
76+
// time
77+
// to
78+
// form
79+
// request
80+
// values
81+
model.addAttribute("dateTimeOutput", enteredDateTime);// add entered date and time values needed to print
82+
// formatted date and time values
83+
return "result";// return result page
6584
}
6685

67-
@RequestMapping("/")
68-
public String showHomePage() {
69-
return "index";
86+
@RequestMapping("/") // show index page when request is made from home page
87+
public String showHomePage() {// show home page
88+
return "index";// return index page
7089
}
7190
}

datetime-thymeleaf/src/main/java/com/example/lulunac27a/datetimethymeleaf/entity/DateTime.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.example.lulunac27a.datetimethymeleaf.entity;
22

3-
public class DateTime {
3+
public class DateTime {// class with date and time information
44
private int year;
55
private int month;
66
private int day;

datetime-thymeleaf/src/main/resources/templates/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ <h1>Print Date and Time in All Supported Formats using Thymeleaf</h1>
9494
<button type="submit">Submit</button>
9595
</form>
9696
<br />
97+
<!--get current date and time-->
9798
<h2>Current Date and Time:</h2>
9899
<h3>Java Date and Time Object:</h3>
99100
Current Era:

datetime-thymeleaf/src/main/resources/templates/result.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<body>
66
<h1>Output:</h1>
77
<div class="container">
8+
<!--get printed date and time in all supported formats using Java Date and Time and Thymeleaf Date and Time objects-->
89
<h3>Java Date and Time Object:</h3>
910
Era:
1011
<span

0 commit comments

Comments
 (0)