英文:
Java - Print First and Last date for each month within specified given month-year range
问题
import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;
public class GetDate_Firt_Last {
static String input_StartMonthYear = "1-2019";
static String input_EndMonthYear = "6-2020";
public static void main (String args[]) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("MM-yyyy");
Calendar startCal = Calendar.getInstance();
startCal.setTime(sdf.parse(input_StartMonthYear));
Calendar endCal = Calendar.getInstance();
endCal.setTime(sdf.parse(input_EndMonthYear));
while (startCal.before(endCal) || startCal.equals(endCal)) {
getFirstDayOfTheMonth(startCal);
getLastDayOfTheMonth(startCal);
System.out.println();
startCal.add(Calendar.MONTH, 1);
}
}
public static String getFirstDayOfTheMonth(Calendar cal) throws Exception {
String firstDay;
try {
int firstDate = cal.getActualMinimum(Calendar.DATE);
int month = cal.get(Calendar.MONTH) + 1;
int year = cal.get(Calendar.YEAR);
firstDay = year + "-" + month + "-" + firstDate;
System.out.println("firstDay-> " +firstDay);
} catch (Exception e) {
throw new Exception("Exception in getFirstDayOfTheMonth." + e.getMessage());
}
return firstDay;
}
public static String getLastDayOfTheMonth(Calendar cal) throws Exception {
String lastDay;
try {
int lastDate = cal.getActualMaximum(Calendar.DATE);
int month = cal.get(Calendar.MONTH) + 1;
int year = cal.get(Calendar.YEAR);
lastDay = year + "-" + month + "-" + lastDate;
System.out.println("lastDay-> " + lastDay);
} catch (Exception e) {
throw new Exception("Exception in getLastDateOfTheMonth." + e.getMessage());
}
return lastDay;
}
}
英文:
How can I print all months first and the last date which falls within the given specified Month-Year range?
So far I have the code which prints for 1 complete year example, if in the input year 2019 is provided then the first and the last date of all 12 months is printed. But what I want is it should print values within the specified Month-Year range.
Example,
String input_StartMonthYear = "1-2019";
String input_EndMonthYear = "6-2020";
Expected Output:
firstDay-> 2019-1-1
lastDay-> 2019-1-31
firstDay-> 2019-2-1
lastDay-> 2019-2-28
---
---
firstDay-> 2020-6-1
lastDay-> 2020-6-30
Code:
import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;
public class GetDate_Firt_Last {
static String input_Year = "2019";
static String completeinput;
static int totalmonths[] = {1,2,3,4,5,6,7,8,9,10,11,12};
public static void main (String args[]) throws Exception
{
int m;
for (m = 1;m <= totalmonths.length;m++)
{
completeinput = m + "-" + input_Year;
SimpleDateFormat sdf = new SimpleDateFormat("MM-yyyy");
Date date = sdf.parse(completeinput);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
getFirstDayOfTheMonth(cal);
getLastDayOfTheMonth(cal);
System.out.println();
}
}
public static String getFirstDayOfTheMonth(Calendar cal) throws Exception {
String firstDay;
try {
int firstDate = cal.getActualMinimum(Calendar.DATE);
int month = cal.get(Calendar.MONTH) +1;
int year = cal.get(Calendar.YEAR);
firstDay = year + "-" + month + "-" + firstDate;
System.out.println("firstDay-> " +firstDay);
} catch (Exception e) {
throw new Exception("Exception in getFirstDayOfTheMonth." + e.getMessage());
}
return firstDay;
}
public static String getLastDayOfTheMonth(Calendar cal) throws Exception {
String lastDay;
try {
int lastDate = cal.getActualMaximum(Calendar.DATE);
int month = cal.get(Calendar.MONTH)+1;
int year = cal.get(Calendar.YEAR);
lastDay = year + "-" + month + "-" + lastDate;
System.out.println("lastDay-> "+ lastDay);
} catch (Exception e) {
throw new Exception("Exception in getLastDateOfTheMonth." + e.getMessage());
}
return lastDay;
}
}
Actual Output:
firstDay-> 2019-1-1
lastDay-> 2019-1-31
firstDay-> 2019-2-1
lastDay-> 2019-2-28
---
---
firstDay-> 2019-12-1
lastDay-> 2019-12-31
答案1
得分: 2
以下是翻译好的代码部分:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
public class GetDate_First_Last {
public static void main(String[] args) {
String input_StartMonthYear = "1-2019";
String input_EndMonthYear = "6-2020";
DateTimeFormatter df = DateTimeFormatter.ofPattern("d-M-yyyy");
LocalDate start = LocalDate.parse("1-" + input_StartMonthYear, df);
LocalDate end = LocalDate.parse("1-" + input_EndMonthYear, df);
for (LocalDate d = start; d.isBefore(end.plusMonths(1)); d = d.plusMonths(1)) {
System.out.println("firstDay -> " + d.with(TemporalAdjusters.firstDayOfMonth()));
System.out.println("lastDay -> " + d.with(TemporalAdjusters.lastDayOfMonth()));
System.out.println();
}
}
}
英文:
If you use java 8 or higher something like this could be a first approach
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
public class GetDate_Firt_Last {
public static void main(String[] args) {
String input_StartMonthYear = "1-2019";
String input_EndMonthYear = "6-2020";
DateTimeFormatter df = DateTimeFormatter.ofPattern("d-M-yyyy");
LocalDate start = LocalDate.parse("1-" +input_StartMonthYear, df);
LocalDate end = LocalDate.parse("1-" + input_EndMonthYear, df);
for(LocalDate d = start; d.isBefore(end.plusMonths(1)); d = d.plusMonths(1)){
System.out.println("firstDay -> " + d.with(TemporalAdjusters.firstDayOfMonth()));
System.out.println("lastDay -> " + d.with(TemporalAdjusters.lastDayOfMonth()));
System.out.println();
}
}
}
答案2
得分: 1
以下是翻译好的部分:
厄立特里亚人的答案很好。如果你喜欢流式解决方案,这里有一个:
DateTimeFormatter monthYearFormatter = DateTimeFormatter.ofPattern("M-u");
Period step = Period.ofMonths(1);
String inputStartMonthYear = "1-2019";
String inputEndMonthYear = "6-2020";
LocalDate startDateInclusive
= YearMonth.parse(inputStartMonthYear, monthYearFormatter).atDay(1);
LocalDate endDateExclusive = YearMonth
.parse(inputEndMonthYear, monthYearFormatter)
.plusMonths(1)
.atDay(1);
startDateInclusive.datesUntil(endDateExclusive, step)
.forEach(d1 -> {
System.out.println("firstDay-> " + d1);
LocalDate endOfMonth = d1.with(TemporalAdjusters.lastDayOfMonth());
System.out.println("lastDay-> " + endOfMonth);
});
输出较长,因此我只提供第一行和最后一行:
> firstDay-> 2019-01-01
> lastDay-> 2019-01-31
> firstDay-> 2019-02-01
> lastDay-> 2019-02-28
> firstDay-> 2019-03-01
> lastDay-> 2019-03-31
> firstDay-> 2019-04-01
> lastDay-> 2019-04-30
> (... 省略 ...)
> firstDay-> 2020-05-01
> lastDay-> 2020-05-31
> firstDay-> 2020-06-01
> lastDay-> 2020-06-30
英文:
Eritrean’s answer is fine. In case you fancy a stream solution, here’s one:
DateTimeFormatter monthYearFormatter = DateTimeFormatter.ofPattern("M-u");
Period step = Period.ofMonths(1);
String inputStartMonthYear = "1-2019";
String inputEndMonthYear = "6-2020";
LocalDate startDateInclusive
= YearMonth.parse(inputStartMonthYear, monthYearFormatter).atDay(1);
LocalDate endDateExclusive = YearMonth
.parse(inputEndMonthYear, monthYearFormatter)
.plusMonths(1)
.atDay(1);
startDateInclusive.datesUntil(endDateExclusive, step)
.forEach(d1 -> {
System.out.println("firstDay-> " + d1);
LocalDate endOfMonth = d1.with(TemporalAdjusters.lastDayOfMonth());
System.out.println("lastDay-> " + endOfMonth);
});
The output is longish, so I am giving you just the first and the last lines:
> firstDay-> 2019-01-01
> lastDay-> 2019-01-31
> firstDay-> 2019-02-01
> lastDay-> 2019-02-28
> firstDay-> 2019-03-01
> lastDay-> 2019-03-31
> firstDay-> 2019-04-01
> lastDay-> 2019-04-30
> (... cut ...)
> firstDay-> 2020-05-01
> lastDay-> 2020-05-31
> firstDay-> 2020-06-01
> lastDay-> 2020-06-30
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论