first Day Of Week - Java java.util

Java examples for java.util:Week

Description

first Day Of Week

Demo Code

/******************************************************************************
 * Copyright (C) 2012 ShenZhen 1000funs Information Technology Co.,Ltd
 * All Rights Reserved.//  w  w w . java 2  s.  c o m
 * ???????????????????????
 *****************************************************************************/
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Vector;

public class Main{
    
    public final static String ISO_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    
    public final static String ISO_DATE_FORMAT = "yyyy-MM-dd";
    
    public static String firstDayOfWeek(int year, int week) {
        Calendar objCalendar = Calendar.getInstance();
        objCalendar.set(Calendar.YEAR, year);
        objCalendar.set(Calendar.WEEK_OF_YEAR, week);
        // ??Calendar.setFirstDayOfWeek()
        objCalendar.add(Calendar.DAY_OF_WEEK,
                (-1 * objCalendar.get(Calendar.DAY_OF_WEEK)) + 2);
        return formatDate(objCalendar.getTime());
    }
    
    public static String formatDate(Date value) {
        return formatDateTime(value, ISO_DATE_FORMAT);
    }
    
    public static String formatDateTime(Date value) {
        return formatDateTime(value, ISO_DATETIME_FORMAT);
    }
    
    public static String formatDateTime(Date value, String defaultFormat) {
        if (value == null) {
            return "";
        }
        String strFormatStyle = StringUtil.isEmpty(defaultFormat) ? ISO_DATETIME_FORMAT
                : defaultFormat;
        SimpleDateFormat objSimpleDateFormat = new SimpleDateFormat(
                strFormatStyle);
        return objSimpleDateFormat.format(value);
    }
}

Related Tutorials