Java如何显示一年的周数?

在Java中,如何查找一年中或一个月中的第几个星期?

以下示例显示年份和月份的第几周。

package com.yiibai;
import java.util.*; public class DisplayWeekNumber { public static void main(String[] args) throws Exception { Date d1 = new Date(); Calendar cl = Calendar.getInstance(); cl.setTime(d1); System.out.println("today is " + cl.WEEK_OF_YEAR + " week of the year"); System.out.println("today is a " + cl.DAY_OF_MONTH + "month of the year"); System.out.println("today is a " + cl.WEEK_OF_MONTH + "week of the month"); } } 
Java

上述代码示例将产生以下结果。

today is 3 week of the year
today is a 5month of the year
today is a 4week of the month
Shell

示例-2
以下是一年中的一个月的一个例子。

package com.yiibai;
import java.util.Calendar; public class DisplayWeekNumber2 { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); System.out.println("Current week of month is : " + cal.get(Calendar.WEEK_OF_MONTH)); System.out.println("Current week of year is : " + cal.get(Calendar.WEEK_OF_YEAR)); cal.add(Calendar.WEEK_OF_MONTH, 1); System.out.println("date after one year : " + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.DATE) + "-" + cal.get(Calendar.YEAR)); } } 
Java

上述代码示例将产生以下结果。

Current week of month is : 4
Current week of year is : 38
date after one year : 9-24-2017

转载于:https://www.cnblogs.com/borter/p/9613418.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值