如何在java中用空格分隔的日期输入中打印日期名称? [英] How do I print the day name from date input separated by spaces in java ?

查看:256
本文介绍了如何在java中用空格分隔的日期输入中打印日期名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你有一个约会。你的任务是找到那天的日期。



输入格式



一行输入,分别包含分别为月,日和年的空格,分别为MM DD YYYY格式。



输出格式



以大写字母输出正确的日期。



我的代码:



You are given a date. Your task is to find what the day is on that date.

Input Format

A single line of input containing the space separated month, day and year, respectively, in MM DD YYYY format.

Output Format

Output the correct day in capital letters.

My code:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int month = in.nextInt();
        int day = in.nextInt();
        int year = in.nextInt();
        Calendar cal = Calendar.getInstance();
        
        cal.set(month,day,year);
        switch(cal.get(cal.DAY_OF_WEEK))
            {
            case 1:System.out.println("SUNDAY");
            break;
            case 2:System.out.println("MONDAY");
            break;
            case 3:System.out.println("TUESDAY");
            break;
            case 4:System.out.println("WEDNESDAY");
            break;
            case 5:System.out.println("THURSDAY");
            break;
            case 6:System.out.println("FRIDAY");
            break;
            case 7:System.out.println("SATURDAY");
            break;
        }
    }
}





我尝试了什么:



以上代码仅在少数测试案例中取得成功,在主要测试案例中失败。

有人可以帮我通知我在哪里制作一个错误?



What I have tried:

The above code yields success only for few test cases and fails for major test cases.
Can someone help me notifying where am I making a mistake ?

推荐答案

你确定



are you sure

cal.set(month,day,year);





是对的吗?我将查看文档日历(Java Platform SE 7) [ ^ ] - 我原本以为使用日历是有点矫枉过正 - 在我的日子里,我们使用Zeller的同意并喜欢它 Zeller的同意 - 维基百科,免费的百科全书 [ ^ ]



is correct ? I'd have a look at the doc Calendar (Java Platform SE 7 )[^] - I would have thought using a calendar for this was overkill - in my day we used Zeller's congruence and liked it Zeller's congruence - Wikipedia, the free encyclopedia[^]


为什么强行打开门? Java支持日期格式化的自定义格式。



检查:自定义格式 [ ^ ]

SimpleDateFormat(Java)平台SE 6) [ ^ ]



此处显示了另一种方式:更改Java字符串中的日期格式 - Stack Overflow [ ^ ]
Why to force doors wide open? Java supports custom formats for date formatting.

Check this: Customizing Formats[^]
SimpleDateFormat (Java Platform SE 6)[^]

Another way is shown here: Change date format in a Java string - Stack Overflow[^]


我遇到过同样的问题,错误的你做的是日callender课程的月份从0到11开始,

所以0 = 1月,

1 = 2月,...

.. < br $> b $ b ......

等......

解决这个问题的方法是..

i have experienced the same problem, the wrong u did was that the months in callender class starts from 0 to 11,
so 0=January,
1= February ,...
..
...
and so on...
the solution to that problem is ..
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
		int month = in.nextInt();
		int day = in.nextInt();
        
        
        int year = in.nextInt();
        Calendar cal = Calendar.getInstance();
          
      /// cal.set(month,day,year);
	  cal.set(cal.YEAR,year);
	  cal.set(cal.MONTH,month-1);
	  cal.set(cal.DATE,day);
//System.out.println("Calendar's year:-- " + cal.get(Calendar.YEAR));
	  // System.out.println("Calendar's month:-- " + cal.get(Calendar.MONTH));
		
		 //  System.out.println("Calendar's Date:-- " + cal.get(Calendar.DATE));
		  // System.out.println( + cal.get(Calendar.DAY_OF_MONTH));
		 //   System.out.println("Calendar's Day:-- " + cal.get(Calendar.DAY_OF_WEEK));
        // switch(cal.get(cal.DAY_OF_WEEK))
		
						switch(cal.get(Calendar.DAY_OF_WEEK))
            {
            case 1:System.out.println("SUNDAY");
            break;
            case 2:System.out.println("MONDAY");
            break;
            case 3:System.out.println("TUESDAY");
            break;
            case 4:System.out.println("WEDNESDAY");
            break;
            case 5:System.out.println("THURSDAY");
            break;
            case 6:System.out.println("FRIDAY");
            break;
            case 7:System.out.println("SATURDAY");
            break;
        }
    }
}


这篇关于如何在java中用空格分隔的日期输入中打印日期名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆