简体   繁体   English

在Java中将月份和年份转换为日期

[英]Converting months and years to date in java

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. 如果在程序中有人给了我几天(ab =“ days”),如果有人给了我几个月(ab =“几个月”),那么我需要将其转换(以“ val”为单位的值)成天,如果有人给我几年(ab =“ years”),那么我需要将其转换为(以“ val”的值)成天。 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'. 即,如果用户在变量“ ab”中指定值的类型(即是月,年还是日期)和变量“ val”中的(天/月/年的天数),则需要获取变量“天”。

For example ab="years" and val = 3 then days is 1095(approximate). 例如ab =“ years”且val = 3,则天数是1095(近似值)。

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");
            }
        }
    }

So you want to keep track of time in java. 因此,您想跟踪Java中的时间。 You should start by reading this: 您应该先阅读以下内容:

https://docs.oracle.com/javase/tutorial/datetime/iso/index.html 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? 为什么将这两次相减(在1927年)会得出奇怪的结果?

and here 和这里

Java string to date conversion Java字符串到日期的转换

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. 除了具有不同天数和leap年的月份外,还存在一些有趣的奥秘问题,例如leap秒。 There are also systems that measure time in milliseconds since jan 1 1970 that don't give a fig about corrections. 自1970年1月1日以来,还有一些以毫秒为单位的时间测量系统,没有给出校正的数字。 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 . 只需说,只需远离java.util.Date并使用java.util.Calendar Date will break your heart . 约会会让你伤心

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM