Java Week getPreWeek(String week)

Here you can find the source of getPreWeek(String week)

Description

get Pre Week

License

Apache License

Declaration

public static String getPreWeek(String week) 

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;
import java.util.GregorianCalendar;

public class Main {
    private static final String LEGACY_FORMAT = "MM/dd/yyyy HH:mm aa";

    public static String getPreWeek(String week) {
        return getDeltaWeek(week, -1);
    }/*from   ww w.j  a v a  2 s. co m*/

    public static String getDeltaWeek(String week, int delta) {
        GregorianCalendar rightNow = new GregorianCalendar();
        try {
            Date date = getDefaultWeekFormat().parse(week);
            rightNow.setTime(date);
            rightNow.add(Calendar.WEEK_OF_YEAR, delta);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return getDefaultWeekFormat().format(rightNow.getTime());
    }

    public static Date parse(String s, String formatPattern) {
        try {
            return new SimpleDateFormat(formatPattern).parse(s);
        } catch (ParseException e) {
            throw new RuntimeException("could not parse date: " + s + " LEGACY_FORMAT = "
                    + new SimpleDateFormat(LEGACY_FORMAT).toPattern(), e);
        }
    }

    public static SimpleDateFormat getDefaultWeekFormat() {
        return new SimpleDateFormat("yyyyww");
    }

    public static String format(Date date, String formatPattern) {
        if (date == null) {
            return "";
        } else {
            return new SimpleDateFormat(formatPattern).format(date);
        }
    }
}

Related

  1. currentWeekEndDate()
  2. getNextWeek(String week)
  3. getPreWeekDayByStr(String curday)
  4. getPreWeekDayByStr(String curday)
  5. getPreWeeks(int weekNum)
  6. getStartDate(Date date, int weeks, Locale locale)