Computer Time is Based on Milliseconds • Generally, we count time in computer software in milliseconds (thousandths of a second) since 1970/01/01 01:00:00 - that is 1AM on 1st January 1970. • You...


Computer Time is Based on Milliseconds<br>• Generally, we count time in computer software in milliseconds (thousandths of a second) since 1970/01/01 01:00:00 - that is 1AM on 1st January 1970.<br>• You might think

Extracted text: Computer Time is Based on Milliseconds • Generally, we count time in computer software in milliseconds (thousandths of a second) since 1970/01/01 01:00:00 - that is 1AM on 1st January 1970. • You might think "that's over 50 years of milliseconds - it must be a huge number". And you'd be right - in fact, the number will get too big to store in "normal" ints in 2038 (not just for Java) - an now uses the long datatype instead of int to store time values (these won't run out for nearly 300 billion years). • Working with time in Java (and other languages) basically involves using built-in libraries to convert time to/from milliseconds. You'll also need (maybe.) to be able to covert milliseconds to seconds (divide by 1000), minutes (divide again by 60) and hours (should be clear what you need to do now). • There are several libraries for this in Java - which is (a) confusing and (b) dangerous since some are now deprecated (as in you shouldn't use them). • We will be using two - LocalDateTime (for actually handling dates) and DateTimeFormatter (for turning strings into dates) - both of which are up to date. • In common with standard practice when we say "date" we mean "date and time". The 'long' Datatype Because the numbers we are dealing with are potentially very large, we need to use long not int. Using long is basically the same as using int except that you generally need to put an Lon the end prevent Java compiler errors because numbers are too large. For example: long example - 1996614713000L; //1998614713000 without the L Nould be too big - the L forces it to be a long DateTimeFormatter We will use the date format: year/month/day hour:mins:secs to represent dates as a String, where each of the components is two digits except year, which is four. To do this we first need to creat DateTimeFormatter of - DatetimeFormatter.ofPattern("yyyy/vad HH: mm:55"); This creates an object df which we can use to both parse dates (turn strings in the above format into milliseconds) and generate date strings (turn milliseconds into strings representing dates). You need to have the import statement: Import java.time.format.DateTineFornatter; in your code LocalDateTime This needs the import statements: import java.time.Instant; import java.time.LocalDateTime; import java.tine.Zoneid; And you'll need two different kinds of code fragments. To turn a String in the format above into milliseconds: long milliseconds - LocalDateTime.parse(date, of).atzone(Zoneld.systemDefault()).toInstant().toEpochtilli(): where date is a string in the format above, and df is the DateTimeFormatter above. Note that the result is a long, not an int! To turn milliseconds back into a String (which depending on how you approach the problems you may not have to do): LocalDatetime dnils - LocalDateTine.ofInstant(Instant.ofEpochMilli (millis), Zoneid.systemDefault()); String datestring- oniis.fornat (of); where millis is a long (not an int!) representing the date in milliseconds. Danger! If you look at the documentation for LocalDateTime you'll find a lot of methods to do with e.g. hours, minutes and seconds - you might think these might come in handy to convert from millisecond you might first think they do and are unlikely to be useful (except to waste valuable time). Q1 Time: Which of the pairs of dates below is separated by 178665918000L milliseconds? O 1998/03/16 22:03:02 and 2003/12/01 08:01:46 1998/03/22 03:40:40 and 2003/11/06 23:47:43 1998/03/29 20:59:25 and 2003/12/11 09:14:32 1998/03/16 13:29:46 and 2003/11/07 22:56:07 O 1998/04/03 01:27:45 and 2003/11/30 21:53:03
Q2<br>Time: Which of the dates below is 735764000L milliseconds from the date 2047/10/29 20:37:58?<br>O 2047/10/29 20:40:03<br>O 2047/11/07 09:00:42<br>O 2047/10/29 20:31:20<br>2047/10/29 20:41:17<br>O 2047/10/29 20:44:39<br>Q3<br>Time: Suppose you are at time traveller and travel through the sequence of dates in the following list,how many milliseconds would you have travelled through? Note<br>that going backwards in time does not mean you 'subtract'milliseconds - the time you travel through (forwards or backwards) always adds on to the time you have<br>travelled through up to that point. For example, if you travelled from 1st Jan 2021 to 1st Jan 2022, and then back to 1st Jan 2021 you would have travelled through<br>two years of time (note though that the question is asking for an answer in milliseconds).<br>String datelist[] - (

Extracted text: Q2 Time: Which of the dates below is 735764000L milliseconds from the date 2047/10/29 20:37:58? O 2047/10/29 20:40:03 O 2047/11/07 09:00:42 O 2047/10/29 20:31:20 2047/10/29 20:41:17 O 2047/10/29 20:44:39 Q3 Time: Suppose you are at time traveller and travel through the sequence of dates in the following list,how many milliseconds would you have travelled through? Note that going backwards in time does not mean you 'subtract'milliseconds - the time you travel through (forwards or backwards) always adds on to the time you have travelled through up to that point. For example, if you travelled from 1st Jan 2021 to 1st Jan 2022, and then back to 1st Jan 2021 you would have travelled through two years of time (note though that the question is asking for an answer in milliseconds). String datelist[] - ("1979/08/02 13:45:22", "2069/88/23 19:08:58", "2004/04/29 23:47:47", "1998/06/27 19:06:14", "1982/03/19 23:08: 20", "2005/12/e4 07:50:11", "2046/e2/17 15:04:55", "1988/e7/22 14:38:36", "2040/09/26 10:29:22", "1985/05/29 e1:11:54", "1979/09/02 00:45:03", "2047/12/26 02:22:25", "1999/02/08 22:04:28", "2064/06/28 08:53:34", *2052/02/21 10:04:27", "2040/11/19 20:17:10", "2053/01/08 21:03:03", "2053/02/07 04:30:32", "2050/05/13 02:44:23", "2021/06/25 14:47:42", "2056/02/01 19:51:28", "2026/11/0 9 17:48:33", "2060/11/21 12:03:58", "2005/10/26 14:17:26", "2060/01/88 e0:02:48", "2035/10/22 19:09:06", "197e/e2/27 07:16:40", "2064/08/01 e7:00:45", "1998/10/18 e9:44:0 2", "2010/07/29 00:31:28", "2003/05/29 e5:37:17", "1974/10/25 02:30:02", "1985/04/10 19:01:32", "2054/02/25 18:55:45", "2058/05/11 01:41:32", "2068/06/10 08:34:34", "2047/ 03/05 17:41:35", "2043/04/16 19:57:30", "2035/12/18 16:09:05", "1984/12/15 13:27:20", "1975/06/09 10:53:59", "2031/12/12 15:08:35", "2025/09/02 e1:49:36", "2048/03/08 e7:4 4:57", "2056/07/07 13:43:48", "2041/11/04 22:25:02", "2052/06/17 05:39:24", "2026/02/03 06:23:53", "1995/10/17 20:44:45", "1977/02/11 16:32:17", 2043/11/03 16:02:18", "19 88/04/08 19:04:12", "2027/06/01 21:30:12", *2028/11/07 16:44:32", "1974/04/07 07:47:36", "2063/09/24 17:38:18", "2043/05/03 12:27:28", "1999/12/17 02:33:33", "1988/03/04 0 0:37:31", "1974/12/24 07:08:55", "2057/03/24 03: 22:31", "1983/03/22 13:48:29", "2028/05/06 06:39:46", "2059/04/23 21:00:12", "1971/12/27 19:52:01"); O 71916709378000 O 71916709878000 O 71916709871000 O 71916709322000 O 71916710154000 Q4 Time: Which of the times in the following list are closest to hour 811836 since 1970/01/01 01:00:00? O 2062/07/17 08:06:40 O 2062/07/19 20:41:57 O 2062/08/25 01:23:54 2062/08/12 13:38:34 2062/08/16 14:02:27
Jun 11, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here