Skip to content

Commit

Permalink
Format code for Thymeleaf application
Browse files Browse the repository at this point in the history
  • Loading branch information
lulunac27a committed Jul 6, 2024
1 parent 021133c commit 5fce2a2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

@Controller
public class DateTimeController {
@GetMapping("/")
@GetMapping("/") // get request from home page
public String dateTimeForm(Model model) {
DateTime dateTime = new DateTime();
DateTime dateTime = new DateTime();// create date and time object
model.addAttribute("enteredDateTime", dateTime);
LocalDateTime currentDateTime = LocalDateTime.now();
LocalDateTime currentDateTime = LocalDateTime.now();// set current date and time to now
dateTime.setYear(currentDateTime.getYear());
dateTime.setMonth(currentDateTime.getMonthValue());
dateTime.setDay(currentDateTime.getDayOfMonth());
Expand All @@ -31,10 +31,18 @@ public String dateTimeForm(Model model) {
dateTime.setNanosecond(currentDateTime.getNano() % 1000);
LocalDateTime dateTimeValues = LocalDateTime.of(dateTime.getYear(), dateTime.getMonth(), dateTime.getDay(),
dateTime.getHour(), dateTime.getMinute(), dateTime.getSecond(),
dateTime.getMillisecond() * 1000000 + dateTime.getMicrosecond() * 1000 + dateTime.getNanosecond());
dateTime.getMillisecond() * 1000000 + dateTime.getMicrosecond() * 1000 + dateTime.getNanosecond());// set
// date
// and
// time
// values
model.addAttribute("currentDateTime", dateTimeValues);
Instant currentUtcDateTime = Instant.now();
LocalDateTime currentUtcDateTimeNow = LocalDateTime.ofInstant(currentUtcDateTime, ZoneId.of("UTC"));
LocalDateTime currentUtcDateTimeNow = LocalDateTime.ofInstant(currentUtcDateTime, ZoneId.of("UTC"));// set
// current
// UTC date
// and time
// to now
DateTime utcDateTimeValues = new DateTime();
utcDateTimeValues.setYear(currentUtcDateTimeNow.getYear());
utcDateTimeValues.setMonth(currentUtcDateTimeNow.getMonthValue());
Expand All @@ -50,22 +58,33 @@ public String dateTimeForm(Model model) {
utcDateTimeValues.getSecond(), utcDateTimeValues.getMillisecond() * 1000000
+ utcDateTimeValues.getMicrosecond() * 1000 + utcDateTimeValues.getNanosecond());
model.addAttribute("currentUtcDateTime", utcDateTimeValuesNow);
return "index";
return "index";// return index page
}

@PostMapping("submit-form")
public String formatDateTime(@ModelAttribute("enteredDateTime") DateTime dateTime, Model model) {
@PostMapping("submit-form") // POST request from submiting the form
public String formatDateTime(@ModelAttribute("enteredDateTime") DateTime dateTime, Model model) {// format date and
// time using date
// and time format
// patterns
model.addAttribute("enteredDateTime", dateTime);
LocalDateTime enteredDateTime = LocalDateTime.of(dateTime.getYear(), dateTime.getMonth(), dateTime.getDay(),
dateTime.getHour(), dateTime.getMinute(), dateTime.getSecond(),
dateTime.getMillisecond() * 1000000 + dateTime.getMicrosecond() * 1000 + dateTime.getNanosecond());
model.addAttribute("enteredDateTime", dateTime);
model.addAttribute("dateTimeOutput", enteredDateTime);
return "result";
dateTime.getMillisecond() * 1000000 + dateTime.getMicrosecond() * 1000 + dateTime.getNanosecond());// set
// entered
// date
// and
// time
// to
// form
// request
// values
model.addAttribute("dateTimeOutput", enteredDateTime);// add entered date and time values needed to print
// formatted date and time values
return "result";// return result page
}

@RequestMapping("/")
public String showHomePage() {
return "index";
@RequestMapping("/") // show index page when request is made from home page
public String showHomePage() {// show home page
return "index";// return index page
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.lulunac27a.datetimethymeleaf.entity;

public class DateTime {
public class DateTime {// class with date and time information
private int year;
private int month;
private int day;
Expand Down
1 change: 1 addition & 0 deletions datetime-thymeleaf/src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ <h1>Print Date and Time in All Supported Formats using Thymeleaf</h1>
<button type="submit">Submit</button>
</form>
<br />
<!--get current date and time-->
<h2>Current Date and Time:</h2>
<h3>Java Date and Time Object:</h3>
Current Era:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<body>
<h1>Output:</h1>
<div class="container">
<!--get printed date and time in all supported formats using Java Date and Time and Thymeleaf Date and Time objects-->
<h3>Java Date and Time Object:</h3>
Era:
<span
Expand Down

0 comments on commit 5fce2a2

Please sign in to comment.