0

我是一名新的 Java 程序员,正在研究先前的建议,以生成一个可以接受 1-365 整数并给出月份和日期的计算器。我不确定如何将每个月解析为单独的变量。完全卡住了。任何帮助将不胜感激。

import java.util.Scanner;

public class principal {
    public static void maxn(String[] args) {
        Scanner input = new Scanner(System.in);

        int x = 0;
        int date;
        if (x < 30) {
            month = "January";
            date = x;
            System.out.println(month + " " + day);
        } else
            x += 31;
        if (31 < x < 58){
            String month = "February";
            day -= x;        

        if (31 < x < 58 < 89) {
            month = "March"
            day -= x;

            if (31 < x < 58 < 89 < 120) {
                month = "April"
                day -= x;

                if (31 < x < 58 < 89 < 120 < 150) ;
                {
                    month = "May"
                    day -= x;

                    if (31 < x < 58 < 89 < 120 < 150 < 180) ;
                    {
                        month = "June"
                        day -= x;

                        if (31 < x < 58 < 89 < 120 < 150 < 180 < 211) {
                            month = "July"
                            day -= x;

                            if (31 < x < 58 < 89 < 120 < 150 < 180 < 211 < 242) {
                                month = "August"
                                day -= x;

                                if (31 < x < 58 < 89 < 120 < 150 < 180 < 211 < 242 < 273) {
                                    month = "September" day -= x;

                                    if (31 < x < 58 < 89 < 120 < 150 < 180 < 211 < 242 < 273 < 303) {
                                        month = "October" day -= x;

                                        if (31 < x < 58 < 89 < 120 < 150 < 180 < 211 < 242 < 273 < 303 < 334) {
                                            month = "November"
                                            day -= x;

                                            if (31 < x < 58 < 89 < 120 < 150 < 180 < 211 < 242 < 273 < 303 < 365) {
                                                month = "December"
                                                day -= x;
                                            }


                                        }
                                    }
                                }
4

2 回答 2

0

这里有两种完全不同的方式来做你想做的事。第一个假设当前年份(ito 闰年),第二个假设它不是闰年。

public static void main(String[] args) throws IOException
{
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  String line = br.readLine();
  while (!line.isEmpty())
  {
     int i = Integer.parseInt(line);

     // way 1
     Calendar c = Calendar.getInstance();
     c.set(Calendar.DAY_OF_YEAR, i);
     System.out.println(c.get(Calendar.DAY_OF_MONTH) + " " +
                        DateFormatSymbols.getInstance().getMonths()[c.get(Calendar.MONTH)]);

     // way 2
     String[] months = {"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
     int[] days = {31,28,31,30,31,30,31,31,30,31,30,31};
     int j = 0;
     while (i > days[j])
        i -= days[j++];
     System.out.println(i + " " + months[j]);

     line = br.readLine();
  }
}
于 2013-02-11T19:15:43.747 回答
0

根据我对你的问题的理解,你会想要这样的东西:

int yourInt = 325; // YOUR NUMBER HERE, BETWEEN 1 and 365
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2013);  //depending on the year you want
cal.set(Calendar.DAY_OF_YEAR, yourInt);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
    Date myDate = sdf.parse(sdf.format(cal.getTime()));
    System.out.println("date : " + myDate.toString());
    System.out.println("month : " + (cal.get(Calendar.MONTH) + 1)); //+1 because January is 0
    System.out.println("day of month : " + cal.get(Calendar.DAY_OF_MONTH));
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

改变年份当然会改变你得到的价值。

使用 325 作为整数,您将获得以下输出:

月 : 11 月中
的一天 : 21
日期 : Thu Nov 21 14:12:57 EST 2013

于 2013-02-11T19:10:18.193 回答