java 设置周一时间_JAVA Calendar设置周一为第一天详解

package com.thinkgem.jeesite.modules.sys.utils;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Calendar;

import java.util.Date;

import java.util.GregorianCalendar;

import java.util.List;

import com.thinkgem.jeesite.modules.sys.entity.DateVersion;

/**

* 获取当前版本

* @author jxxt

*

*/

public class DateVersionUtils {

private static Integer WEEKNUM = 13;

public static DateVersion get(){

DateVersion dv = new DateVersion();

SimpleDateFormat simdf = new SimpleDateFormat("MM-dd");

Calendar calendar = Calendar.getInstance();

Calendar c = new GregorianCalendar();

c.setFirstDayOfWeek(Calendar.MONDAY);

c.setMinimalDaysInFirstWeek(7);

Date today = new Date();

c.setTime (today);

// 获取当前年

dv.setYear(calendar.get(Calendar.YEAR));

// 获取当前月

dv.setMonth(calendar.get(Calendar.MONTH) + 1);

// 获取当前日

dv.setDay(calendar.get(Calendar.DATE));

// 获取该周当天

dv.setToday(simdf.format(calendar.getTime()));

// 获取该周周一

calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

dv.setMonday(simdf.format(calendar.getTime()));

// 获取该周周末

calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + 6);

dv.setSunday(simdf.format(calendar.getTime()));

dv.setWeek(c.get(Calendar.WEEK_OF_YEAR));

if(dv.getWeek() > 51){

dv.setYear(calendar.get(Calendar.YEAR)-1);

}

return dv;

}

public static DateVersion returnVersion(int num){

DateVersion dv = new DateVersion();

SimpleDateFormat simdf = new SimpleDateFormat("MM-dd");

//Calendar calendar = Calendar.getInstance();

Calendar c = new GregorianCalendar();

c.setFirstDayOfWeek(Calendar.MONDAY);

c.setMinimalDaysInFirstWeek(7);

@SuppressWarnings("deprecation")

Date today = new Date(""+num+"/12/31");

// 获取当前年

dv.setYear(num);

// 获取当前是第几周

//calendar.setFirstDayOfWeek(Calendar.MONDAY);

System.out.println(today);

c.setTime (today);

dv.setWeek(c.get(Calendar.WEEK_OF_YEAR));

return dv;

}

// 获取当前时间所在年的周数

public static int getWeekOfYear(Date date) {

Calendar c = new GregorianCalendar();

c.setFirstDayOfWeek(Calendar.MONDAY);

c.setMinimalDaysInFirstWeek(1);

c.setTime(date);

return c.get(Calendar.WEEK_OF_YEAR);

}

// 取前13周的计算方法

public static List perv13Week(){

String currentVersion = DateVersionUtils.get().getCurrentVersion();

String[] split = currentVersion.split("-");

List list = new ArrayList();

Integer valueOf = Integer.valueOf(split[1]);

// 如果周大于13

if( valueOf >= WEEKNUM) {

list.add(split[0]+""+split[1]);

for(int i=1; i< WEEKNUM; i++){

if(valueOf > 10){

list.add(split[0]+""+(--valueOf));

} else {

list.add(split[0]+"0"+(--valueOf));

}

}

} else {

// 取得当前周和13的差

int s= WEEKNUM-valueOf;

int num = valueOf;

list.add(split[0]+"0"+split[1]);

for(int i=1; i< num; i++){

if(valueOf > 10){

list.add(split[0]+""+(--valueOf));

} else {

list.add(split[0]+"0"+(--valueOf));

}

}

Integer years = Integer.valueOf(split[0]);

--years;

DateVersion returnVersion = returnVersion(years);

String currentVersion2 = returnVersion.getCurrentVersion();

String[] split2 = currentVersion2.split("-");

Integer valueOf1 = Integer.valueOf(split2[1]);

list.add(split2[0]+""+(valueOf1));

for(int a=1; a

list.add(split2[0]+""+(--valueOf1));

}

}

return list;

}

public static int getDayOfWeek(String dateString){

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

try {

Date date = format.parse(dateString);

Calendar cal = Calendar.getInstance();

cal.setTime(date);

cal.setFirstDayOfWeek(Calendar.MONDAY);

int tmp = cal.get(Calendar.DAY_OF_WEEK) - 1;

if (0 == tmp) {

tmp = 7;

}

return tmp;

} catch (Exception e) {

e.printStackTrace();

return -1;

}

}

public static void main(String[] args) {

String currentVersion = DateVersionUtils.get().getCurrentVersion();

System.out.println("sdf:"+currentVersion);

// System.out.print(DateVersionUtils.get().getCurrentVersion());

List perv13Week = DateVersionUtils.perv13Week();

System.out.println(perv13Week);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值