Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to output the number of days between two dates in Java interesting exercises

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to output the number of days between two dates in Java interesting exercises". In the operation of the actual case, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. needs and ideas

First of all, the requirement of the topic is to compare the two dates and find out the difference in days, then we should first think of the Date date class and use the getTime method of the date class to take out the millisecond value of the two dates. Then subtract this millisecond value to get a millisecond difference, and then divide it by 1000'60'60'24, which is the expression that converts the millisecond value to the day we are familiar with.

Second, code implementation

First of all, we should guide the package to do this problem, Date. We also use a class SimpleDateFormat class to define our format

Public class Time {public static void main (String [] args) throws ParseException {SimpleDateFormat sp = new SimpleDateFormat ("yyyy-MM-dd"); Time.fun (sp);} public static void fun (SimpleDateFormat sp) throws ParseException {/ / start date Date startTime = sp.parse ("2010-09-20"); / / end date Date endTime = sp.parse ("2010-09-21") / / long betweenTime = (endTime.getTime ()-startTime.getTime ()) / (1000 * 60 * 60 * 24); System.out.println (startTime + "\ t" to\ t "+ endTime +"\ t total difference: "+ betweenTime +" days ");}}

This completes our code, which uses a parse method, a method of the SimpleDateFormat class, to parse the text from the beginning of a given string to generate a date.

Finally, take a look at the output:

Mon Sep 20 00:00:00 CST 2010 to Tue Sep 21 00:00:00 CST 2010 total difference: 1 day

We found that the output of this topic is not so good-looking, so then we will add a little bit of code to beautify it.

We can write a class, write a method for String types to convert Date types to each other, and then use this method to define the format we need in the main method.

Package com.API.HomeWork03; import java.text.ParseException;import java.util.Date;import java.text.SimpleDateFormat; public class Utility {private Utility () {} public static String dateToSrting (Date d, String format) {SimpleDateFormat sdf = new SimpleDateFormat (format); String s = sdf.format (d); return s;} public static Date StringToDate (String s, String format) throws ParseException {SimpleDateFormat sdf = new SimpleDateFormat (format) Date d = sdf.parse (s); return d;}}

This is a utility utility class we have written, the user converts the date format, notice that here we have a throws ParseException, which is that we have thrown an exception.

The rest is our main test class, such as the format I set here

String start = Utility.dateToSrting (startTime, "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy

Just call the method of our tool class, pass in the date object we need to modify, then pass in the format, and receive it with the String type.

To make the code more interesting, we can set the date to be entered by the user, so that the program is more playable.

Public class Time {public static void main (String [] args) throws ParseException {SimpleDateFormat sp = new SimpleDateFormat ("yyyy-MM-dd"); Time.fun (sp);} public static void fun (SimpleDateFormat sp) throws ParseException {Scanner sc = new Scanner (System.in); System.out.println ("Please enter start date: (format: 2010-09-20); String sta = sc.next () System.out.println ("Please enter an end date: (format: 2010-09-20)"); String en = sc.next (); / / start date Date startTime = sp.parse (sta); / / end date Date endTime = sp.parse (en); String start = Utility.dateToSrting (startTime, "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy String end = Utility.dateToSrting (endTime, "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy

Example of output

Please enter the start date: (format: 2010-09-20)

2020-9-11

Please enter the end date: (format: 2010-09-20)

2020-9-30

Total difference from September 11, 2020 to September 30, 2020: 19 days

You can see that this part of our code has become perfect.

Conclusion

For example, this problem can solve more problems.

For example, calculate how many days and years a person has been born

And how many days from today to so-and-so can be solved.

Part of the source code utility utility class package com.API.HomeWork03; import java.text.ParseException;import java.util.Date;import java.text.SimpleDateFormat; / * * @ author wang * / public class Utility {private Utility () {} public static String dateToSrting (Date d, String format) {SimpleDateFormat sdf = new SimpleDateFormat (format); String s = sdf.format (d); return s } public static Date StringToDate (String s, String format) throws ParseException {SimpleDateFormat sdf = new SimpleDateFormat (format); Date d = sdf.parse (s); return d;}} Time test class package com.API.HomeWork03; import java.text.SimpleDateFormat;import java.util.Date;import java.text.ParseException;import java.util.Scanner; / * * 4. Calculate the number of days between two dates *

* for example: write a method (fun3 ("2010-09-20", "2010-09-21") 2010-09-20 2010-09-21 * * @ author wang * / public class Time {public static void main (String [] args) throws ParseException {SimpleDateFormat sp = new SimpleDateFormat ("yyyy-MM-dd"); Time.fun (sp);} public static void fun (SimpleDateFormat sp) throws ParseException {Scanner sc = new Scanner (System.in) System.out.println ("Please enter start date: (format: 2010-09-20)"); String sta = sc.next (); System.out.println ("Please enter end date: (format: 2010-09-20)"); String en = sc.next (); / / start date Date startTime = sp.parse (sta) / / end date Date endTime = sp.parse (en); String start = Utility.dateToSrting (startTime, "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy System.out.println (start + "\ t" to\ t "+ end +"\ t total difference: "+ betweenTime +" days ");}}" Java interesting exercises how to output the number of days between two dates "is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report