Skip to content

Commit 4df3fff

Browse files
committed
Format Java code
1 parent e089959 commit 4df3fff

File tree

3 files changed

+431
-203
lines changed

3 files changed

+431
-203
lines changed
Lines changed: 63 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
package com.example.lulunac27a.datetimethymeleaf;
22

3+
import com.example.lulunac27a.datetimethymeleaf.entity.DateTime;
4+
import java.time.Instant;
5+
import java.time.LocalDateTime;
6+
import java.time.ZoneId;
37
import org.springframework.stereotype.Controller;
48
import org.springframework.ui.Model;
59
import org.springframework.web.bind.annotation.GetMapping;
610
import org.springframework.web.bind.annotation.ModelAttribute;
711
import org.springframework.web.bind.annotation.PostMapping;
812
import org.springframework.web.bind.annotation.RequestMapping;
913

10-
import com.example.lulunac27a.datetimethymeleaf.entity.DateTime;
11-
12-
import java.time.Instant;
13-
import java.time.LocalDateTime;
14-
import java.time.ZoneId;
15-
1614
@Controller
1715
public class DateTimeController {
16+
1817
@GetMapping("/") // get request from home page
1918
public String dateTimeForm(Model model) {
20-
DateTime dateTime = new DateTime();// create the date and time object
19+
DateTime dateTime = new DateTime(); // create the date and time object
2120
model.addAttribute("enteredDateTime", dateTime);
22-
LocalDateTime currentDateTime = LocalDateTime.now();// set the current date and time to now
21+
LocalDateTime currentDateTime = LocalDateTime.now(); // set the current date and time to now
2322
dateTime.setYear(currentDateTime.getYear());
2423
dateTime.setMonth(currentDateTime.getMonthValue());
2524
dateTime.setDay(currentDateTime.getDayOfMonth());
@@ -29,62 +28,78 @@ public String dateTimeForm(Model model) {
2928
dateTime.setMillisecond((currentDateTime.getNano() / 1000000) % 1000);
3029
dateTime.setMicrosecond((currentDateTime.getNano() / 1000) % 1000);
3130
dateTime.setNanosecond(currentDateTime.getNano() % 1000);
32-
LocalDateTime dateTimeValues = LocalDateTime.of(dateTime.getYear(), dateTime.getMonth(), dateTime.getDay(),
33-
dateTime.getHour(), dateTime.getMinute(), dateTime.getSecond(),
34-
dateTime.getMillisecond() * 1000000 + dateTime.getMicrosecond() * 1000 + dateTime.getNanosecond());// set
35-
// date
36-
// and
37-
// time
38-
// values
31+
LocalDateTime dateTimeValues = LocalDateTime.of(
32+
dateTime.getYear(),
33+
dateTime.getMonth(),
34+
dateTime.getDay(),
35+
dateTime.getHour(),
36+
dateTime.getMinute(),
37+
dateTime.getSecond(),
38+
dateTime.getMillisecond() *
39+
1000000 +
40+
dateTime.getMicrosecond() *
41+
1000
42+
+
43+
dateTime.getNanosecond()); // set date and time values
3944
model.addAttribute("currentDateTime", dateTimeValues);
4045
Instant currentUtcDateTime = Instant.now();
41-
LocalDateTime currentUtcDateTimeNow = LocalDateTime.ofInstant(currentUtcDateTime, ZoneId.of("UTC"));// set the
42-
// current
43-
// UTC date
44-
// and time
45-
// to now
46+
LocalDateTime currentUtcDateTimeNow = LocalDateTime.ofInstant(
47+
currentUtcDateTime,
48+
ZoneId.of("UTC")); // set the current UTC date and time to now
4649
DateTime utcDateTimeValues = new DateTime();
4750
utcDateTimeValues.setYear(currentUtcDateTimeNow.getYear());
4851
utcDateTimeValues.setMonth(currentUtcDateTimeNow.getMonthValue());
4952
utcDateTimeValues.setDay(currentUtcDateTimeNow.getDayOfMonth());
5053
utcDateTimeValues.setHour(currentUtcDateTimeNow.getHour());
5154
utcDateTimeValues.setMinute(currentUtcDateTimeNow.getMinute());
5255
utcDateTimeValues.setSecond(currentUtcDateTimeNow.getSecond());
53-
utcDateTimeValues.setMillisecond((currentUtcDateTimeNow.getNano() / 1000000) % 1000);
54-
utcDateTimeValues.setMicrosecond((currentUtcDateTimeNow.getNano() / 1000) % 1000);
56+
utcDateTimeValues.setMillisecond(
57+
(currentUtcDateTimeNow.getNano() / 1000000) % 1000);
58+
utcDateTimeValues.setMicrosecond(
59+
(currentUtcDateTimeNow.getNano() / 1000) % 1000);
5560
utcDateTimeValues.setNanosecond(currentUtcDateTimeNow.getNano() % 1000);
56-
LocalDateTime utcDateTimeValuesNow = LocalDateTime.of(utcDateTimeValues.getYear(), utcDateTimeValues.getMonth(),
57-
utcDateTimeValues.getDay(), utcDateTimeValues.getHour(), utcDateTimeValues.getMinute(),
58-
utcDateTimeValues.getSecond(), utcDateTimeValues.getMillisecond() * 1000000
59-
+ utcDateTimeValues.getMicrosecond() * 1000 + utcDateTimeValues.getNanosecond());
61+
LocalDateTime utcDateTimeValuesNow = LocalDateTime.of(
62+
utcDateTimeValues.getYear(),
63+
utcDateTimeValues.getMonth(),
64+
utcDateTimeValues.getDay(),
65+
utcDateTimeValues.getHour(),
66+
utcDateTimeValues.getMinute(),
67+
utcDateTimeValues.getSecond(),
68+
utcDateTimeValues.getMillisecond() *
69+
1000000 +
70+
utcDateTimeValues.getMicrosecond() *
71+
1000
72+
+
73+
utcDateTimeValues.getNanosecond());
6074
model.addAttribute("currentUtcDateTime", utcDateTimeValuesNow);
61-
return "index";// return index page
75+
return "index"; // return index page
6276
}
6377

6478
@PostMapping("submit-form") // POST request from submitting 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
79+
public String formatDateTime(
80+
@ModelAttribute("enteredDateTime") DateTime dateTime,
81+
Model model) { // format date and time using date and time format patterns
6982
model.addAttribute("enteredDateTime", dateTime);
70-
LocalDateTime enteredDateTime = LocalDateTime.of(dateTime.getYear(), dateTime.getMonth(), dateTime.getDay(),
71-
dateTime.getHour(), dateTime.getMinute(), dateTime.getSecond(),
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
83+
LocalDateTime enteredDateTime = LocalDateTime.of(
84+
dateTime.getYear(),
85+
dateTime.getMonth(),
86+
dateTime.getDay(),
87+
dateTime.getHour(),
88+
dateTime.getMinute(),
89+
dateTime.getSecond(),
90+
dateTime.getMillisecond() *
91+
1000000 +
92+
dateTime.getMicrosecond() *
93+
1000
94+
+
95+
dateTime.getNanosecond()); // set entered date and time to form request values
96+
model.addAttribute("dateTimeOutput", enteredDateTime); // add entered date and time values needed to print
97+
// formatted date and time values
98+
return "result"; // return result page
8499
}
85100

86101
@RequestMapping("/") // show index page when request is made from home page
87-
public String showHomePage() {// show home page
88-
return "index";// return index page
102+
public String showHomePage() { // show home page
103+
return "index"; // return index page
89104
}
90-
}
105+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.example.lulunac27a.datetimethymeleaf.entity;
22

3-
public class DateTime {// class with date and time information with milliseconds, microseconds and
4-
// nanoseconds
3+
public class DateTime { // class with date and time information with milliseconds, microseconds and
4+
// nanoseconds
5+
56
private int year;
67
private int month;
78
private int day;
@@ -84,5 +85,4 @@ public int getNanosecond() {
8485
public void setNanosecond(int nanosecond) {
8586
this.nanosecond = nanosecond;
8687
}
87-
88-
}
88+
}

0 commit comments

Comments
 (0)