Skip to content

Commit 5bd222b

Browse files
committed
Add time with milliseconds, microseconds and nanoseconds precision
1 parent 902a7de commit 5bd222b

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

datetime-jsp/datetime.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
<input type="number" name="minute" id="minute" min="0" max="59" required><br>
1919
<label for="second">Second:</label><br>
2020
<input type="number" name="second" id="second" min="0" max="59" required><br>
21+
<label for="millisecond">Millisecond:</label><br>
22+
<input type="number" name="millisecond" id="millisecond" min="0" max="999" required value="0"><br>
23+
<label for="microsecond">Microsecond:</label><br>
24+
<input type="number" name="microsecond" id="microsecond" min="0" max="999" required value="0"><br>
25+
<label for="nanosecond">Nanosecond:</label><br>
26+
<input type="number" name="nanosecond" id="nanosecond" min="0" max="999" required value="0"><br>
2127
<button type="button" onclick="setCurrentDate()">Set Current Local Date and Time</button>
2228
<button type="button" onclick="setCurrentUtcDate()">Set Current UTC Date and Time</button>
2329
<input type="submit" value="Submit">

datetime-jsp/datetime.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ var day = document.querySelector("#day");
44
var hour = document.querySelector("#hour");
55
var minute = document.querySelector("#minute");
66
var second = document.querySelector("#second");
7+
var millisecond = document.querySelector("#millisecond");
8+
var microsecond = document.querySelector("#microsecond");
9+
var nanosecond = document.querySelector("#nanosecond");
710
function setCurrentDate() {
811
var currentDate = new Date();
912
var currentYear = currentDate.getFullYear();
@@ -12,12 +15,16 @@ function setCurrentDate() {
1215
var currentHour = currentDate.getHours();
1316
var currentMinute = currentDate.getMinutes();
1417
var currentSecond = currentDate.getSeconds();
18+
var currentMillisecond = currentDate.getMilliseconds();
1519
year.value = currentYear;//set the input values to current local date and time
1620
month.value = currentMonth;
1721
day.value = currentDay;
1822
hour.value = currentHour;
1923
minute.value = currentMinute;
2024
second.value = currentSecond;
25+
millisecond.value = currentMillisecond;
26+
microsecond.value = 0;
27+
nanosecond.value = 0;
2128
}
2229
function setCurrentUtcDate() {
2330
var currentDate = new Date();
@@ -27,10 +34,14 @@ function setCurrentUtcDate() {
2734
var currentUtcHour = currentDate.getUTCHours();
2835
var currentUtcMinute = currentDate.getUTCMinutes();
2936
var currentUtcSecond = currentDate.getUTCSeconds();
37+
var currentUtcMillisecond = currentDate.getUTCMilliseconds();
3038
year.value = currentUtcYear;//set the input values to current UTC date and time
3139
month.value = currentUtcMonth;
3240
day.value = currentUtcDay;
3341
hour.value = currentUtcHour;
3442
minute.value = currentUtcMinute;
3543
second.value = currentUtcSecond;
44+
millisecond.value = currentUtcMillisecond;
45+
microsecond.value = 0;
46+
nanosecond.value = 0;
3647
}

datetime-jsp/datetime.jsp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ int day = Integer.parseInt(request.getParameter("day"));
1717
int hour = Integer.parseInt(request.getParameter("hour"));
1818
int minute = Integer.parseInt(request.getParameter("minute"));
1919
int second = Integer.parseInt(request.getParameter("second"));
20+
int millisecond = Integer.parseInt(request.getParameter("millisecond"));
21+
int microsecond = Integer.parseInt(request.getParameter("microsecond"));
22+
int nanosecond = Integer.parseInt(request.getParameter("nanosecond"));
2023
GregorianCalendar datetime = new GregorianCalendar(year, month, day, hour, minute, second); // Get the date and time values based on user entered input
21-
LocalDateTime dateandtime = LocalDateTime.of(year, month, day, hour, minute, second); // Convert datetime GregorianCalendar object to LocalDateTime object named dateandtime without time zone information
24+
LocalDateTime dateandtime = LocalDateTime.of(year, month, day, hour, minute, second).withNano(millisecond * 1000000 + microsecond * 1000 + nanosecond); // Convert datetime GregorianCalendar object to LocalDateTime object named dateandtime without time zone information
2225
DateTimeFormatter era1 = DateTimeFormatter.ofPattern("GGG"); // Format the date and time values with specified pattern
2326
DateTimeFormatter era2 = DateTimeFormatter.ofPattern("GGGG");
2427
DateTimeFormatter era3 = DateTimeFormatter.ofPattern("GGGGG");

datetime.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ public static void main(String[] args) {
1818
int minute = sc.nextInt();
1919
System.out.print("Second: ");
2020
int second = sc.nextInt();
21+
System.out.print("Millisecond: ");
22+
int millisecond = sc.nextInt();
23+
System.out.print("Microsecond: ");
24+
int microsecond = sc.nextInt();
25+
System.out.print("Nanosecond: ");
26+
int nanosecond = sc.nextInt();
2127
GregorianCalendar datetime = new GregorianCalendar(year, month, day, hour, minute, second); // Get the
2228
// date and
2329
// time
@@ -26,16 +32,16 @@ public static void main(String[] args) {
2632
// user
2733
// entered
2834
// input
29-
LocalDateTime dateandtime = LocalDateTime.of(year, month, day, hour, minute, second); // Convert
30-
// datetime of
31-
// GregorianCalendar
32-
// object to
33-
// LocalDateTime
34-
// object named
35-
// dateandtime
36-
// without time
37-
// zone
38-
// information
35+
LocalDateTime dateandtime = LocalDateTime.of(year, month, day, hour, minute, second)
36+
.withNano(millisecond * 1000000 + microsecond * 1000 + nanosecond); // Convert datetime
37+
// of
38+
// GregorianCalendar
39+
// object to
40+
// LocalDateTime
41+
// object named
42+
// dateandtime
43+
// without time zone
44+
// information
3945
DateTimeFormatter era1 = DateTimeFormatter.ofPattern("GGG"); // Format the date and time values with
4046
// specified pattern
4147
DateTimeFormatter era2 = DateTimeFormatter.ofPattern("GGGG");

0 commit comments

Comments
 (0)