Skip to content

Commit

Permalink
Format Java code
Browse files Browse the repository at this point in the history
  • Loading branch information
lulunac27a committed Nov 7, 2024
1 parent e089959 commit 4df3fff
Show file tree
Hide file tree
Showing 3 changed files with 431 additions and 203 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package com.example.lulunac27a.datetimethymeleaf;

import com.example.lulunac27a.datetimethymeleaf.entity.DateTime;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.example.lulunac27a.datetimethymeleaf.entity.DateTime;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

@Controller
public class DateTimeController {

@GetMapping("/") // get request from home page
public String dateTimeForm(Model model) {
DateTime dateTime = new DateTime();// create the date and time object
DateTime dateTime = new DateTime(); // create the date and time object
model.addAttribute("enteredDateTime", dateTime);
LocalDateTime currentDateTime = LocalDateTime.now();// set the current date and time to now
LocalDateTime currentDateTime = LocalDateTime.now(); // set the current date and time to now
dateTime.setYear(currentDateTime.getYear());
dateTime.setMonth(currentDateTime.getMonthValue());
dateTime.setDay(currentDateTime.getDayOfMonth());
Expand All @@ -29,62 +28,78 @@ public String dateTimeForm(Model model) {
dateTime.setMillisecond((currentDateTime.getNano() / 1000000) % 1000);
dateTime.setMicrosecond((currentDateTime.getNano() / 1000) % 1000);
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());// set
// date
// and
// time
// values
LocalDateTime dateTimeValues = LocalDateTime.of(
dateTime.getYear(),
dateTime.getMonth(),
dateTime.getDay(),
dateTime.getHour(),
dateTime.getMinute(),
dateTime.getSecond(),
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"));// set the
// current
// UTC date
// and time
// to now
LocalDateTime currentUtcDateTimeNow = LocalDateTime.ofInstant(
currentUtcDateTime,
ZoneId.of("UTC")); // set the current UTC date and time to now
DateTime utcDateTimeValues = new DateTime();
utcDateTimeValues.setYear(currentUtcDateTimeNow.getYear());
utcDateTimeValues.setMonth(currentUtcDateTimeNow.getMonthValue());
utcDateTimeValues.setDay(currentUtcDateTimeNow.getDayOfMonth());
utcDateTimeValues.setHour(currentUtcDateTimeNow.getHour());
utcDateTimeValues.setMinute(currentUtcDateTimeNow.getMinute());
utcDateTimeValues.setSecond(currentUtcDateTimeNow.getSecond());
utcDateTimeValues.setMillisecond((currentUtcDateTimeNow.getNano() / 1000000) % 1000);
utcDateTimeValues.setMicrosecond((currentUtcDateTimeNow.getNano() / 1000) % 1000);
utcDateTimeValues.setMillisecond(
(currentUtcDateTimeNow.getNano() / 1000000) % 1000);
utcDateTimeValues.setMicrosecond(
(currentUtcDateTimeNow.getNano() / 1000) % 1000);
utcDateTimeValues.setNanosecond(currentUtcDateTimeNow.getNano() % 1000);
LocalDateTime utcDateTimeValuesNow = LocalDateTime.of(utcDateTimeValues.getYear(), utcDateTimeValues.getMonth(),
utcDateTimeValues.getDay(), utcDateTimeValues.getHour(), utcDateTimeValues.getMinute(),
utcDateTimeValues.getSecond(), utcDateTimeValues.getMillisecond() * 1000000
+ utcDateTimeValues.getMicrosecond() * 1000 + utcDateTimeValues.getNanosecond());
LocalDateTime utcDateTimeValuesNow = LocalDateTime.of(
utcDateTimeValues.getYear(),
utcDateTimeValues.getMonth(),
utcDateTimeValues.getDay(),
utcDateTimeValues.getHour(),
utcDateTimeValues.getMinute(),
utcDateTimeValues.getSecond(),
utcDateTimeValues.getMillisecond() *
1000000 +
utcDateTimeValues.getMicrosecond() *
1000
+
utcDateTimeValues.getNanosecond());
model.addAttribute("currentUtcDateTime", utcDateTimeValuesNow);
return "index";// return index page
return "index"; // return index page
}

@PostMapping("submit-form") // POST request from submitting the form
public String formatDateTime(@ModelAttribute("enteredDateTime") DateTime dateTime, Model model) {// format date and
// time using date
// and time format
// patterns
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());// 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
LocalDateTime enteredDateTime = LocalDateTime.of(
dateTime.getYear(),
dateTime.getMonth(),
dateTime.getDay(),
dateTime.getHour(),
dateTime.getMinute(),
dateTime.getSecond(),
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("/") // show index page when request is made from home page
public String showHomePage() {// show home page
return "index";// return index page
public String showHomePage() { // show home page
return "index"; // return index page
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.example.lulunac27a.datetimethymeleaf.entity;

public class DateTime {// class with date and time information with milliseconds, microseconds and
// nanoseconds
public class DateTime { // class with date and time information with milliseconds, microseconds and
// nanoseconds

private int year;
private int month;
private int day;
Expand Down Expand Up @@ -84,5 +85,4 @@ public int getNanosecond() {
public void setNanosecond(int nanosecond) {
this.nanosecond = nanosecond;
}

}
}
Loading

0 comments on commit 4df3fff

Please sign in to comment.