Java Calendar 初级应用

本文介绍如何使用Java中的Calendar和SimpleDateFormat类来获取当前日期时间、年份、月份、周数及周内天数等信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Calendar cal = Calendar.getInstance();//初始化


SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA); //设置显示格式,地区

 

Date currentTime = new Date();    String dateString = formatter.format(currentTime);   //取得今天时间


int year = cal.get(Calendar.YEAR);//取得年


int mouth = cal.get(Calendar.MONTH) + 1;//取得月


int week = cal.get(Calendar.WEEK_OF_MONTH); //取得周

 

int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK)//取得每周的第几天

 

int dayOfMouth = cal.get(Calendar.DAY_OF_MOUTH)//取得每周的第几天

 

# 農曆 Class **農曆** class is in the *org.magiclen.農曆* package. ### Initialize You can't create a **農曆** instance by using **new** operator directly. You have to use the **建立** static method provided by **農曆** class to create a **農曆** instance. You can pass the year, month and the days of the month in Gregorian calendar as parameters to **建立** method. For example, we want to create a **農曆** object instance with the date 2015-12-16 in Gregorian calendar. The code will be, 農曆.建立(2015, 12, 12); For example, we want to create a **農曆** object instance with the date 2015-12-16 in Chinese calendar and we know that date is not in a leap month. The code will be, 農曆 date = 農曆.建立(2015, 12, false, 12); ### Get the date in Gregorian calendar Use **取得西曆** method to get the date in Gregorian calendar. String gregorianDate = date.取得西曆(); ### Get the date in Chinese calendar Use **取得農曆** method to get the date in Chinese calendar. String chineseDate = date.取得農曆(); ### Compute the weight of Ba Zi(八字) Use **計算八字有幾兩重** method to compute the weight of Ba Zi. Because **農曆** object has no hour information, if you want to compute the weight of Ba Zi, you have to provide the hour information in the form of the twelve Earthly Branches. float BaZiWeight = date.計算八字有幾兩重(農曆.地支.); # Run **Java Chinese Calendar** as a program You can also use the two commands below to run **Java Chinese Calendar**: java -jar JavaChineseCalendar.jar It will show today's date in Gregorian calendar and Chinese calendar. The result just likes 西曆:2015-12-16 農曆:2015(乙未、羊年) 冬月 初六 Yo
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值