欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java獲取本月日歷表的方法

 更新時(shí)間:2022年06月13日 15:11:51   作者:最長(zhǎng)的電影!  
這篇文章主要為大家詳細(xì)介紹了java獲取本月日歷表的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

用Java獲取本月日歷表,供大家參考,具體內(nèi)容如下

這張日歷表應(yīng)該怎么打印出來(lái)?

首先,獲取本月,獲取本天,創(chuàng)建本地日期類(lèi)對(duì)象。

LocalDate date=LocalDate.now();
? ?int month=date.getMonthValue();//獲取本月
? ?int today=date.getDayOfMonth();//獲取本天

2.創(chuàng)建一個(gè)周期對(duì)象。

date=date.minusDays(today-1);//將date設(shè)置為這個(gè)月的第一天
? DayOfWeek weekday=date.getDayOfWeek();
? int value=weekday.getValue();//得到星期幾的一個(gè)數(shù)值,1就返回1,2就是2.......

3.然后獲取的月份等于本月份,按照間隔輸出,等于本月那一天,就用*特殊標(biāo)記那一天。

for(int i=1;i<value;i++) {
?? ??? ??? ?System.out.print(" ? ?");
?? ??? ?}
while(date.getMonthValue()==month) {
?? ??? ??? ?System.out.printf("%3d", date.getDayOfMonth());
?? ??? ??? ?if(date.getDayOfMonth()==today) {
?? ??? ??? ??? ?System.out.print("*");
?? ??? ??? ?}
?? ??? ??? ?else {
?? ??? ??? ??? ?System.out.print(" ");
?? ??? ??? ?}
?? ??? ??? ?date=date.plusDays(1);

4.如果如果獲取到的周天數(shù)是星期一了,那就換行。

if(date.getDayOfWeek().getValue()==1) {
?? ??? ??? ??? ?System.out.println();
?? ??? ??? ?}

這樣本月日歷表就被獲取出來(lái)了。

完整代碼如下:

import java.time.*;
public class CalendarTest {

?? ?public static void main(String[] args) {
?? ??? ?// TODO 自動(dòng)生成的方法存根
?? ??? ?LocalDate date=LocalDate.now();
?? ??? ?int month=date.getMonthValue();//獲取本月
?? ??? ?int today=date.getDayOfMonth();//獲取本天
?? ??? ?
?? ??? ?date=date.minusDays(today-1);//將date設(shè)置為這個(gè)月的第一天
?? ??? ?DayOfWeek weekday=date.getDayOfWeek();
?? ??? ?int value=weekday.getValue();//得到星期幾的一個(gè)數(shù)值,1就返回1,2就是2.......
? ? ? ? System.out.println("Mon Tue Wed Thu Fri Sat Sun");
?? ??? ?for(int i=1;i<value;i++) {
?? ??? ??? ?System.out.print(" ? ?");
?? ??? ?}
?? ??? ?while(date.getMonthValue()==month) {
?? ??? ??? ?System.out.printf("%3d", date.getDayOfMonth());
?? ??? ??? ?if(date.getDayOfMonth()==today) {
?? ??? ??? ??? ?System.out.print("*");
?? ??? ??? ?}
?? ??? ??? ?else {
?? ??? ??? ??? ?System.out.print(" ");
?? ??? ??? ?}
?? ??? ??? ?date=date.plusDays(1);
?? ??? ??? ?if(date.getDayOfWeek().getValue()==1) {
?? ??? ??? ??? ?System.out.println();
?? ??? ??? ?}
?? ??? ?}
?? ?}
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論