Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class HelloWorld {
- public static void main(String args[]) {
- Scanner s = new Scanner(System.in);
- System.out.print("Enter starting date separated by dots: ");
- String inp1 = s.nextLine();
- System.out.print("Enter ending date separated by dots: ");
- String inp2 = s.nextLine();
- int[] nodim = {
- 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
- };
- String[] inpArr1 = split(inp1);
- String[] inpArr2 = split(inp2);
- int d1 = Integer.parseInt(inpArr1[0]);
- int m1 = Integer.parseInt(inpArr1[1]);
- int y1 = Integer.parseInt(inpArr1[2]);
- int d2 = Integer.parseInt(inpArr2[0]);
- int m2 = Integer.parseInt(inpArr2[1]);
- int y2 = Integer.parseInt(inpArr2[2]);
- if (y1 % 4 == 0) nodim[2] = 29;
- int diff = m1 == m2 && y1 == y2 ? d2 - d1 : (nodim[m1] - d1) + d2;
- int mm1 = m1 + 1, mm2 = m2 - 1, yy1 = y1, yy2 = y2;
- for (; yy1 <= yy2; yy1++, mm1 = 1) {
- mm2 = yy1 == yy2 ? (m2 - 1) : 12;
- if (yy1 % 4 == 0) nodim[2] = 29;
- else nodim[2] = 28;
- if (mm2 == 0) {
- mm2 = 12;
- yy2 = yy2 - 1;
- }
- for (; mm1 <= mm2 && yy1 <= yy2; mm1++) diff = diff + nodim[mm1];
- }
- System.out.print("No. of days from " + inp1 + " to " + inp2 + " is " + diff);
- }
- public static String[] split(String s) {
- String[] retval = {
- "", "", ""
- };
- s = s + ".";
- s = s + " ";
- for (int i = 0; i <= 2; i++) {
- retval[i] = s.substring(0, s.indexOf("."));
- s = s.substring((s.indexOf(".") + 1), s.length());
- }
- return retval;
- }
- }
Add Comment
Please, Sign In to add comment