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) 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 e prevent Java compiler errors because numbers are too large. For example: long exanple 1998614713000L; /1998614713000 wdthout 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 cr Datetimefornatter of - Datetineformatter.ofPattern("yy/vad HHim:ss"): 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: Irport java. tine.fornat.DateTimeFormatters in your code LocalDateTime This needs the import statements: import java.tine.Instant; Smport java.time.LocalDetetime inport java.tine. Zonetd And you'll need two different kinds of code fragments. To turn a String in the format above into milliseconds: long milliseconds - LocalDatetine.parse(date, of).atzone(Zoneia. systenoefault()).tolnstant().totpoci110: 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): LocalDetetime dniis - LocalDetetine.ofInstant(Instant.ofpochni11i(nil1is), Zoneid.systenbefault()); 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 millisec 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? 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 O 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
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) 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 e prevent Java compiler errors because numbers are too large. For example: long exanple 1998614713000L; /1998614713000 wdthout 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 cr Datetimefornatter of - Datetineformatter.ofPattern("yy/vad HHim:ss"): 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: Irport java. tine.fornat.DateTimeFormatters in your code LocalDateTime This needs the import statements: import java.tine.Instant; Smport java.time.LocalDetetime inport java.tine. Zonetd And you'll need two different kinds of code fragments. To turn a String in the format above into milliseconds: long milliseconds - LocalDatetine.parse(date, of).atzone(Zoneia. systenoefault()).tolnstant().totpoci110: 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): LocalDetetime dniis - LocalDetetine.ofInstant(Instant.ofpochni11i(nil1is), Zoneid.systenbefault()); 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 millisec 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? 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 O 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
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
Related questions
Question
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 4 steps with 7 images
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY