0

If in the program someone gives me the number of days(ab="days") I need to set the variable 'days' as the same number(value in 'val'), if some one gives me the months(ab="months") then I need to convert(value in 'val') it into days, if someone gives me years(ab="years") then I need to convert(value in 'val') it into days. ie if the user specifies the type of values(ie whether it is month or year or date) in variable 'ab' and the number (of days/months/years) in variable 'val', I need to get number of days in the variable 'days'.

For example ab="years" and val = 3 then days is 1095(approximate).

What I tried is as below. Is it the right approach for the above problem? Thanks in advance.

public class conversionofdate {
        public static void main(String args[]) {
            String ab = "days";
            int val = 0, days, month = 0, years = 0;
            switch (ab) {
                case "days":
                    days = val;
                    break;
                case "months":
                    days = val * 30;
                    break;

                case "years":
                    days = val * 12 * 30;
                    break;
                default:
                    System.out.println("Incorrect value");
            }
        }
    }
akshaivk
  • 427
  • 5
  • 24
  • 3
    You cannot infer proper duration periods unless you use specific dates. For instance, how many days are there in February depends on the year, and how many days are between two months depends on which months and year in question. You should probably stick to good implementation of these type of classes in libraries like Joda Time or in the new JDK 8 date time API. And even with those you may get difficulties if you need hisoric accuracy, like dates before Gregorian calendar or dates before AD, etc. – Edwin Dalorzo Nov 17 '14 at 04:57
  • 1
    Since the comments in the deleted question were the only thing that made it clear that this is not a homework question please update the question to make clear this is about solving the real world problem of keeping track of time. – candied_orange Nov 17 '14 at 05:23

1 Answers1

3

So you want to keep track of time in java. You should start by reading this:

https://docs.oracle.com/javase/tutorial/datetime/iso/index.html

And you should be aware that this is not a simple problem. Date and Time have subtle nuances that lead to mind bending bugs as explored here:

Why is subtracting these two times (in 1927) giving a strange result?

and here

Java string to date conversion

Also, be aware that not everyone on the planet uses the gregorian calendar most of us english speakers use. In addition to months having different numbers of days and leap years there are also fun arcane issues like leap seconds. There are also systems that measure time in milliseconds since jan 1 1970 that don't give a fig about corrections. It's great glorious fun trying to sort out where the error is when these different systems get turned into strings in documents that get read into computers that use a different system.

In short the answer to your question is beyond the scope of what can be done justice to in a stack overflow answer. Suffice it to say, just stay away from java.util.Date and use java.util.Calendar. Date will break your heart.

Community
  • 1
  • 1
candied_orange
  • 7,036
  • 2
  • 28
  • 62