如何知道当月(包括)当月剩余的一周中的特定日期
例如今天是2015年4月14日:
本周有几天到月底:
Mondays
= 2的数量
Tuesdays
= 3的数量
Wednesdays
= 3的数量
Thursdays
= 3的数量
Fridays
= 2的数量
Saturdays
= 2的数量
Sundays
= 2
答案 0 :(得分:0)
这是我使用Java 8 public static void getDaysLeftInMonth() {
LocalDate endOfMonth = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
DayOfWeek[] daysOfWeek = DayOfWeek.values();
for (int i = 0; i < daysOfWeek.length; i++) {
int numberOfWeekDaysLeftInMonth = 0;
LocalDate now = LocalDate.now();
while (now.with(TemporalAdjusters.next(daysOfWeek[i])).isBefore(endOfMonth)
|| now.with(TemporalAdjusters.next(daysOfWeek[i])).isEqual(endOfMonth)) {
numberOfWeekDaysLeftInMonth++;
now = now.plusWeeks(1);
}
System.out.printf("%d %sS left\n", numberOfWeekDaysLeftInMonth, daysOfWeek[i].name());
}
}
API快速解决的解决方案。你需要对此进行单元测试:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void LoadNames(object sender, EventArgs e)
{
const int size = 200;
string[] names = new string[size];
string[] names1 = new string[size];
int index = 0;
int index1 = 0;
StreamReader inputfile1;
StreamReader inputfile2;
inputfile1 = File.OpenText(@"F:\C# HW\CH07_HW_07\BoysNames.txt");
inputfile2 = File.OpenText(@"F:\C# HW\CH07_HW_07\GirlsNames.txt");
while (!inputfile1.EndOfStream && index < names.Length)
{
names[index] = inputfile1.ReadLine();
index++;
}
while (!inputfile2.EndOfStream && index1 < names1.Length)
{
names1[index1] = inputfile2.ReadLine();
index1++;
}
}
private Boolean FindBoyname()
{
const int size = 200;
string[] BoyNames = new string[size];
int index = 0;
string boyname = textBox1.Text;
Boolean Boyname = false;
StreamReader inputfile1;
inputfile1 = File.OpenText(@"F:\C# HW\CH07_HW_07\BoysNames.txt");
while (!inputfile1.EndOfStream && index < BoyNames.Length)
{
BoyNames[index] = inputfile1.ReadLine();
if (String.Equals(boyname, BoyNames[index], StringComparison.OrdinalIgnoreCase) == true)
{
Boyname = true;
}
index++;
}
return Boyname;
}
private Boolean FindGirlname()
{
const int size = 200;
string[] GirlNames = new string[size];
int index = 0;
string girlname = textBox2.Text;
Boolean Girlname = false;
StreamReader inputfile1;
inputfile1 = File.OpenText(@"F:\C# HW\CH07_HW_07\GirlsNames.txt");
while (!inputfile1.EndOfStream && index < GirlNames.Length)
{
GirlNames[index] = inputfile1.ReadLine();
if (String.Equals(girlname, GirlNames[index], StringComparison.OrdinalIgnoreCase) == true)
{
Girlname = true;
}
index++;
}
return Girlname;
}
private void button1_Click(object sender, EventArgs e)
{
Boolean boy;
Boolean girl;
boy = FindBoyname();
girl = FindGirlname();
if (boy.Equals(true))
{
MessageBox.Show(textBox1.Text + " is among the most popular boy names!");
}
if (boy.Equals(false))
{
MessageBox.Show(textBox1.Text + " is not among the most popular boy names.");
}
if (girl.Equals(true))
{
MessageBox.Show(textBox2.Text + " is among the most popular girl names!");
}
if (girl.Equals(false))
{
MessageBox.Show(textBox2.Text + " is not among the most popular girl names.");
}
}
}
答案 1 :(得分:0)
真的非常感谢罗伯特。我只是稍微改了一下
public static void getDaysLeftInMonth() {
LocalDate today = new LocalDate("2015-04-22");
LocalDate endOfMonth = today.dayOfMonth().withMaximumValue();;
for (int i = 1; i <= 7; i++) {
int numberOfWeekDaysLeftInMonth = 0;
LocalDate now = today;
LocalDate checkedDay = null;
while ((checkedDay = now.withDayOfWeek(i)).isBefore(endOfMonth.plusDays(1)) ) {
if (checkedDay.isAfter(today.minusDays(1))) {
numberOfWeekDaysLeftInMonth++;
}
now = now.plusWeeks(1);
}
Log.d("TIME", String.format("%d - %d left\n", i, numberOfWeekDaysLeftInMonth));
}
}