Here you can find the source of getWeekFromDate(String dateString)
public static String getWeekFromDate(String dateString)
//package com.java2s; /*/* ww w . j ava 2 s . com*/ * Copyright [2011] [C.H Li http://code.google.com/p/idocbox-common/] * Licensed to the Chunhui Li(C.H Li) under one or more contributor license agreements. * See the NOTICE file distributed with this work for additional information * regarding copyright ownership. * * C.H Li licenses this file to You 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.Date; public class Main { public static String getWeekFromDate(String dateString) { StringBuffer sb = new StringBuffer(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); try { Date date = format.parse(dateString); Date mondy = new Date(); Date sundy = new Date(); int day = date.getDay(); if (day == 0) { mondy.setTime(date.getTime() - 1000 * 60 * 60 * 24 * 6); sundy.setTime(date.getTime()); } else { mondy.setTime(date.getTime() - 1000 * 60 * 60 * 24 * (day - 1)); sundy.setTime(date.getTime() + 1000 * 60 * 60 * 24 * (7 - day)); } sb.append(format.format(mondy)); sb.append("_"); sb.append(format.format(sundy)); } catch (Exception e) { e.printStackTrace(); } return sb.toString(); } }