如何查找一周中的哪一天是 - Java [英] How to find which day of the week it is - Java

查看:131
本文介绍了如何查找一周中的哪一天是 - Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

还有一些其他问题几乎相同,但我似乎无法让我的代码工作。我得到的用户输入是那里生日dd / mm / yyyy。我忽略了闰年。我试图确定用户出生时的星期几。我必须确定他们从某个日期出生的日子,在这个例子中是1901年1月的星期二。这就是为什么ive完成 year-1901

there has been a few other question which are pretty much the same, but i cant seem to get my bit of code to work. I get the user input which is there birthday dd/mm/yyyy. I'm ignoring leap years by the way. Im trying to determine what day of the week it was when the user was born. I have to determine how many days they were born from a certain date which in this case is Tuesday the 1st of January 1901. That's why ive done year-1901

day = int day;

使用开关来确定每月的许多天由 dayMonth
代表,所以7月份有31,feb有28等。

used a switch to determine how many days in each month which is represented by dayMonth so July has 31, feb has 28 etc.

year = int year;

String[] days =
         {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
howManyDays = ((year-1901)*365 + dayMonth + day - 1);       
whatDay = (howManyDays%7);
days[whatDay]

它有时工作,但有时它不会。
如有任何问题,请随时问问任何帮助。感谢提前,希望是有道理的!

It works sometimes, but then sometimes it doesn't. Any help is appreciated, if any questions feel free to ask. Thanks in advance and hope it makes sense!

推荐答案

您可以使用Java日历。

You can use Java Calendar.

Calendar c = Calendar.getInstance();
c.set(year, month, day);

int day_of_week = c.get(Calendar.DAY_OF_WEEK);

这给你一个int的星期几,你可以提供一个数组来映射天的名字。

This gives you an int which day of week it is, you can just provide an array to map with the "names" of the days.

这篇关于如何查找一周中的哪一天是 - Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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