From 5fce2a25c714df071995c76ca53ebe14817adefd Mon Sep 17 00:00:00 2001 From: lulunac27a <100660343+lulunac27a@users.noreply.github.com> Date: Fri, 5 Jul 2024 20:29:42 -0500 Subject: [PATCH] Format code for Thymeleaf application --- .../datetimethymeleaf/DateTimeController.java | 49 +++++++++++++------ .../datetimethymeleaf/entity/DateTime.java | 2 +- .../src/main/resources/templates/index.html | 1 + .../src/main/resources/templates/result.html | 1 + 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/datetime-thymeleaf/src/main/java/com/example/lulunac27a/datetimethymeleaf/DateTimeController.java b/datetime-thymeleaf/src/main/java/com/example/lulunac27a/datetimethymeleaf/DateTimeController.java index 7080a25..f2e0655 100644 --- a/datetime-thymeleaf/src/main/java/com/example/lulunac27a/datetimethymeleaf/DateTimeController.java +++ b/datetime-thymeleaf/src/main/java/com/example/lulunac27a/datetimethymeleaf/DateTimeController.java @@ -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()); @@ -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()); @@ -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 } } \ No newline at end of file diff --git a/datetime-thymeleaf/src/main/java/com/example/lulunac27a/datetimethymeleaf/entity/DateTime.java b/datetime-thymeleaf/src/main/java/com/example/lulunac27a/datetimethymeleaf/entity/DateTime.java index 50f5750..f1498bf 100644 --- a/datetime-thymeleaf/src/main/java/com/example/lulunac27a/datetimethymeleaf/entity/DateTime.java +++ b/datetime-thymeleaf/src/main/java/com/example/lulunac27a/datetimethymeleaf/entity/DateTime.java @@ -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; diff --git a/datetime-thymeleaf/src/main/resources/templates/index.html b/datetime-thymeleaf/src/main/resources/templates/index.html index 1d4a757..f1ba84b 100644 --- a/datetime-thymeleaf/src/main/resources/templates/index.html +++ b/datetime-thymeleaf/src/main/resources/templates/index.html @@ -94,6 +94,7 @@

Print Date and Time in All Supported Formats using Thymeleaf


+

Current Date and Time:

Java Date and Time Object:

Current Era: diff --git a/datetime-thymeleaf/src/main/resources/templates/result.html b/datetime-thymeleaf/src/main/resources/templates/result.html index a3ea275..7cbea8d 100644 --- a/datetime-thymeleaf/src/main/resources/templates/result.html +++ b/datetime-thymeleaf/src/main/resources/templates/result.html @@ -5,6 +5,7 @@

Output:

+

Java Date and Time Object:

Era: