Java Week getWeekBegin(Date date)

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

Description

get Week Begin

License

Apache License

Declaration

public static Date getWeekBegin(Date date) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static Date getWeekBegin(Date date) {
        return getWeekBegin(date, 0);
    }/*  www .  j  a va  2  s  . co m*/

    public static Date getWeekBegin(Date date, Integer day) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0) {
            day_of_week = 7;
        }
        c.add(Calendar.DATE, -day_of_week + 1 + day);

        return stringToDate(dateToString(c.getTime(), "yyyy-MM-dd") + " 00:00:00", "yyyy-MM-dd HH:mm:ss");
    }

    public static Date stringToDate(String dateString) {
        return stringToDate(dateString, "yyyy-MM-dd HH:mm:ss");
    }

    public static Date stringToDate(String dateString, String pattern) {
        SimpleDateFormat simpledateformat = new SimpleDateFormat(pattern);
        Date date = new Date();
        try {
            date = simpledateformat.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String dateToString(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.format(date);
        }
        return "";
    }

    public static String dateToString(Date date) {
        return dateToString(date, "yyyy-MM-dd HH:mm:ss");
    }
}

Related

  1. getWeek(int duration)
  2. getWeek(SimpleDateFormat df, String dateStr)
  3. getWeek(String date)
  4. getWeek(String pTime)
  5. getWeekAgo()
  6. getWeekDates(int weekOffset)
  7. getWeekFirst(String format)
  8. getWeekFromDate(String dateString)
  9. getWeekId(String date)