Java Timestamp weekNumber(Timestamp stamp, TimeZone timeZone, Locale locale)

Here you can find the source of weekNumber(Timestamp stamp, TimeZone timeZone, Locale locale)

Description

week Number

License

Apache License

Declaration

public static int weekNumber(Timestamp stamp, TimeZone timeZone, Locale locale) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.sql.Timestamp;

import java.util.Calendar;
import java.util.Date;

import java.util.Locale;
import java.util.TimeZone;

public class Main {
    /**// www.j  a v  a2 s.co  m
     * returns a week number in a year for a Timestamp input
     *
     * @param input
     *            Timestamp date
     * @return A int containing the week number
     */
    public static int weekNumber(Timestamp input) {
        return weekNumber(input, TimeZone.getDefault(), Locale.getDefault());
    }

    public static int weekNumber(Timestamp input, int startOfWeek) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(startOfWeek);

        if (startOfWeek == Calendar.MONDAY) {
            calendar.setMinimalDaysInFirstWeek(4);
        } else if (startOfWeek == Calendar.SUNDAY) {
            calendar.setMinimalDaysInFirstWeek(3);
        }

        calendar.setTime(new java.util.Date(input.getTime()));
        return calendar.get(Calendar.WEEK_OF_YEAR);
    }

    public static int weekNumber(Timestamp stamp, TimeZone timeZone, Locale locale) {
        Calendar tempCal = toCalendar(stamp, timeZone, locale);
        return tempCal.get(Calendar.WEEK_OF_YEAR);
    }

    public static java.util.Calendar toCalendar(java.sql.Timestamp stamp) {
        Calendar cal = Calendar.getInstance();
        if (stamp != null) {
            cal.setTimeInMillis(stamp.getTime());
        }
        return cal;
    }

    /**
     * Returns a Calendar object initialized to the specified date/time, time
     * zone, and locale.
     *
     * @param date
     *            date/time to use
     * @param timeZone
     * @param locale
     * @return Calendar object
     * @see java.util.Calendar
     */
    public static Calendar toCalendar(Date date, TimeZone timeZone, Locale locale) {
        Calendar cal = Calendar.getInstance(timeZone, locale);
        if (date != null) {
            cal.setTime(date);
        }
        return cal;
    }
}

Related

  1. truncateFractionalSeconds(Timestamp timestamp)
  2. truncateTimestamp(Timestamp timestamp, String period)
  3. truncNanos(Timestamp timestamp)
  4. unformattedFromTimestamp(java.sql.Timestamp t)
  5. utcToTimestamp(String utcTimestamp)
  6. writeTimestamp(ByteBuffer logBuf, Timestamp time)
  7. writeTimestamp(Timestamp stamp, ObjectOutput out, long nullFlag)