Here you can find the source of getWeek(Date today, int index, String format)
public static String getWeek(Date today, int index, String format)
//package com.java2s; //License from project: Apache License import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; public class Main { /**/*ww w . j a v a 2 s . c o m*/ * The constant FORMAT. */ public static final String FORMAT = "yyyy-MM-dd HH:mm:ss"; /** * The constant SDF_FORMAT. */ public static final SimpleDateFormat SDF_FORMAT = new SimpleDateFormat(FORMAT); public static String getWeek(Date today, int index, String format) { Calendar calendar = Calendar.getInstance(); calendar.setTime(today); ArrayList<Date> list = getWeekDateList(today, 0); Date d = calendar.getTime(); for (Date date : list) { Calendar c = Calendar.getInstance(); c.setTime(date); if (c.get(Calendar.DAY_OF_WEEK) == index + 1) { d = c.getTime(); } } return getDateTime(d, format); } /** * Gets week date list. * * @param today the today * @param index the index * @return ArrayList<Date> week date list * @throws * @Title: getWeekDateList * @Description: */ public static ArrayList<Date> getWeekDateList(Date today, int index) { ArrayList<Date> list = new ArrayList<Date>(); Calendar calendar = Calendar.getInstance(); calendar.setTime(today); if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { calendar.add(Calendar.WEEK_OF_YEAR, -1); } calendar.add(Calendar.WEEK_OF_YEAR, index); calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); for (int i = 0; i <= 6; i++) { Calendar c = Calendar.getInstance(); c.setTime(calendar.getTime()); c.add(Calendar.DAY_OF_MONTH, i); Date d = c.getTime(); list.add(d); } if (list.size() == 0) { list.add(today); } return list; } /** * Gets date time. * * @param @return * @return String date time * @throws * @Title: getDateTime * @Description: */ public static String getDateTime() { return SDF_FORMAT.format(new Date()); } /** * Gets date time. * * @param format the format * @return String date time * @throws * @Title: getDateTime * @Description: */ public static String getDateTime(String format) { // Calendar calendar = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat(format); // return sdf.format(calendar.getTime()); return sdf.format(new Date()); } /** * Gets date time. * * @param date the date * @param format the format * @return String date time * @throws * @Title: getDateTime * @Description: */ public static String getDateTime(Date date, String format) { // Calendar calendar = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat(format); // return sdf.format(calendar.getTime()); return sdf.format(date); } /** * Gets date time. * * @param date the date * @return String date time * @throws * @Title: getDateTime * @Description: */ public static String getDateTime(Date date) { // Calendar calendar = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat(FORMAT); // return sdf.format(calendar.getTime()); return SDF_FORMAT.format(date); } /** * Gets date time. * * @param today the today * @param index the index * @param format the format * @return the date time */ public static String getDateTime(Date today, int index, String format) { Calendar calendar = Calendar.getInstance(); calendar.setTime(today); calendar.add(Calendar.DAY_OF_MONTH, index); Date d = calendar.getTime(); return getDateTime(d, format); } /** * Gets date time. * * @param currentTimeMillis the current time millis * @param format the format * @return String date time * @throws * @Title: getDateTime * @Description: */ public static String getDateTime(long currentTimeMillis, String format) { SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(new Date(currentTimeMillis)); } /** * Gets date time. * * @param currentTimeMillis the current time millis * @return String date time * @throws * @Title: getDateTime * @Description: */ public static String getDateTime(long currentTimeMillis) { SimpleDateFormat sdf = new SimpleDateFormat(FORMAT); return sdf.format(new Date(currentTimeMillis)); } /** * Gets date time. * * @param currentTimeMillis the current time millis * @param ms the ms * @return String date time * @throws * @Title: getDateTime * @Description: */ public static String getDateTime(long currentTimeMillis, boolean ms) { String format = ms ? "yyyy-MM-dd HH:mm:ss.SSS" : FORMAT; SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(new Date(currentTimeMillis)); } /** * Format string. * * @param format the format * @param date the date * @return the string * @throws ParseException the parse exception */ public static String format(String format, Date date) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(date); } /** * Format string. * * @param date the date * @return the string * @throws ParseException the parse exception */ public static String format(Date date) throws ParseException { return SDF_FORMAT.format(date); } }