Java Week getThisWeek()

Here you can find the source of getThisWeek()

Description

get This Week

License

Open Source License

Declaration

public static List<String> getThisWeek() 

Method Source Code

//package com.java2s;
/**//from www . java  2 s . c  om
 * Copyright (c)2010-2011 Enterprise Website Content Management System(EWCMS), All rights reserved.
 * EWCMS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * http://www.ewcms.com
 */

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class Main {
    private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";

    public static List<String> getThisWeek() {
        return getThisWeek(DEFAULT_DATE_FORMAT);
    }

    public static List<String> getThisWeek(String format) {
        List<String> list = new ArrayList<String>();
        try {
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat simple = new SimpleDateFormat(format);
            Date current = new Date(Calendar.getInstance().getTime().getTime());

            calendar.setTime(current);
            int weekDay = calendar.get(Calendar.DAY_OF_WEEK) == 1 ? 8 : calendar.get(Calendar.DAY_OF_WEEK);
            calendar.add(Calendar.DATE, Calendar.MONDAY - weekDay);
            Date start = calendar.getTime();
            calendar.add(Calendar.DATE, 6);
            Date end = calendar.getTime();

            Long mid = end.getTime() - start.getTime() + 1;
            int day = (int) (mid / (1000 * 60 * 60 * 24));

            calendar.setTime(start);
            list.add(simple.format(start.getTime()));
            for (int i = 0; i < day; i++) {
                calendar.add(Calendar.DATE, 1);
                list.add(simple.format(calendar.getTime()));
            }
        } catch (Exception e) {
        }
        return list;
    }
}

Related

  1. getPreWeekDayByStr(String curday)
  2. getPreWeekDayByStr(String curday)
  3. getPreWeeks(int weekNum)
  4. getStartDate(Date date, int weeks, Locale locale)
  5. getStartDateOfWeek(Date selectedDate, Locale locale)
  6. getThisWeek()
  7. getWeek()
  8. getWeek()
  9. getWeek(Date date)