Java Week getWeek(Date date)

Here you can find the source of getWeek(Date date)

Description

get Week

License

Apache License

Declaration

public static Date[] getWeek(Date date) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2015 htd0324@gmail.com/*from  ww w  . ja va  2  s.com*/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static Date[] getWeek(Date date) {
        Date[] dates = new Date[7];
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        for (int i = 0; i < 7; i++) {
            dates[i] = calendar.getTime();
            calendar.add(Calendar.DAY_OF_YEAR, 1);
        }
        return dates;
    }

    public static String[] getWeek(Date date, String pattern) {
        Date[] dates = getWeek(date);
        String[] result = new String[dates.length];
        for (int i = 0; i < 7; i++) {
            result[i] = date2String(dates[i], pattern);
        }
        return result;
    }

    public static String date2String(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
        formatter.setTimeZone(TimeZone.getDefault());
        return formatter.format(date);
    }

    public static String date2String(Date date, String pattern) {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        formatter.setTimeZone(TimeZone.getDefault());
        return formatter.format(date);
    }
}

Related

  1. getStartDateOfWeek(Date selectedDate, Locale locale)
  2. getThisWeek()
  3. getThisWeek()
  4. getWeek()
  5. getWeek()
  6. getWeek(int duration)
  7. getWeek(SimpleDateFormat df, String dateStr)
  8. getWeek(String date)
  9. getWeek(String pTime)