java:用LocalDate给自己做一个消费清单并生成txt保存
// 因为感觉自己每天乱花钱 就想着把未来每日消费做个清单 本来还在"yyyy年MM月dd日"的打字
// 突然想起来自己是程序员 立马扇自己两巴掌 这不是可以用程序实现么?
// 先创建一个类
public static void main(String[] args) throws IOException {
getMothsDays(12L);
}
/**
* 通过给定月数量获取到当前时间的天数
* @param mothNum 未来月数量
*/
public static void getMothsDays(Long mothNum) throws IOException {
LocalDate now = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
// 当前时间月天数
int indexCount = now.lengthOfMonth();
int dayOfMonth = now.getDayOfMonth();
System.out.println("当前月剩余:" + (indexCount - dayOfMonth));
// 未来某个月天数
int lastCount = 0;
for (int i = 1; i <= mothNum; i++) {
LocalDate lastMoth = LocalDate.now().plusMonths(i);
lastCount += lastMoth.lengthOfMonth();
}
System.out.println("未来月合计天数" + lastCount);
int count = (indexCount - dayOfMonth) + lastCount;
System.out.println("当前月剩余天数加下一个月天数共计:" + count);
LocalDate localDate = null;
StringBuffer sb = new StringBuffer();
for (int i = 0; i < count; i++) {
localDate = now.plusDays(i);
sb.append(formatter.format(localDate) + "\n");
}
// 总结
String endStr = formatter.format(now) + "到" + formatter.format(localDate) + "账单";
sb.append(endStr);
FileOutputStream fos = new FileOutputStream("D://" + endStr + ".txt");
fos.write(sb.toString().getBytes());
fos.close();
效果图:
每天消费记个帐 看看自己未来某天是否超支。
。。。。等等