Java Week getWeekFromDate(String dateString)

Here you can find the source of getWeekFromDate(String dateString)

Description

get Week From Date

License

Apache License

Declaration

public static String getWeekFromDate(String dateString) 

Method Source Code

//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();
    }
}

Related

  1. getWeek(String pTime)
  2. getWeekAgo()
  3. getWeekBegin(Date date)
  4. getWeekDates(int weekOffset)
  5. getWeekFirst(String format)
  6. getWeekId(String date)
  7. getWeekInMonth()
  8. getWeekName()
  9. getWeekNumber(String sdate)