Java Week Day getSQLForWeekSunday(String weekString)

Here you can find the source of getSQLForWeekSunday(String weekString)

Description

getSQLForWeekSunday: sql for sunday

License

Apache License

Parameter

Parameter Description
today any day

Return

Date with no time {talendTypes} String {Category} TimestampUtil {param} string week: week in yyyy-ww. {example} getSQLForWeekSunday(week).

Declaration

public static String getSQLForWeekSunday(String weekString)
        throws java.text.ParseException 

Method Source Code

//package com.java2s;
/**/*from w w  w . j  a  va 2  s  . co m*/
 * Copyright 2015 Jan Lolling jan.lolling@gmail.com
 * 
 * Licensed 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.Calendar;

import java.util.Locale;

public class Main {
    /**
     * getSQLForWeekSunday: sql for sunday
     * @param today any day
     * @return Date with no time
     * 
     * {talendTypes} String
     * 
     * {Category} TimestampUtil
     * 
     * {param} string week: week in yyyy-ww.
     * 
     * {example} getSQLForWeekSunday(week).
     */
    public static String getSQLForWeekSunday(String weekString)
            throws java.text.ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-ww", new Locale(
                "de", "DE"));// we use DIN 1355/ISO 8601 weeks
        java.util.Date startDate = getNextSunday(sdf.parse(weekString));
        SimpleDateFormat sdfOutput = new SimpleDateFormat("dd.MM.yyyy");
        return "to_date('" + sdfOutput.format(startDate)
                + "','DD.MM.YYYY')";
    }

    public static java.util.Date getNextSunday(java.util.Date today) {
        if (today != null) {
            java.util.Calendar c = java.util.Calendar
                    .getInstance(Locale.GERMANY);
            c.setTime(today);
            c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
            c.set(java.util.Calendar.MINUTE, 0);
            c.set(java.util.Calendar.SECOND, 0);
            c.set(java.util.Calendar.MILLISECOND, 0);
            c.set(java.util.Calendar.HOUR_OF_DAY, 0);
            return c.getTime();
        } else {
            return null;
        }
    }

    /**
     * format: returns the formatted date as String.
     * 
     * {talendTypes} String
     * 
     * {Category} TimestampUtil
     * 
     * {param} Date(date) date : date to format
     * 
     * {param} String(pattern) pattern : date to format
     * 
     * {example} format(date, "dd.MM.yyyy")
     */
    public static String format(java.util.Date date, String pattern) {
        if (date != null) {
            return new SimpleDateFormat(pattern).format(date);
        } else {
            return null;
        }
    }
}

Related

  1. getPreviousMonday(Date date, int days)
  2. getPreviousOrNextDay(String time, int dayNum)
  3. getPreWeekLastDay()
  4. getShortWeekday(int dayOfWeek)
  5. getShortWeekday(String day)
  6. getSundayOfWeek()
  7. getThisWeekMonday()
  8. getTodayOfWeek()
  9. getWeek(Date today, int index, String format)