java.util下的Calendar类

官方文档:jse8 https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#getFirstDayOfWeek--

所有方法:

 
Modifier and TypeMethod and Description
abstract voidadd(int field, int amount)

Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules.

booleanafter(Object when)

Returns whether this Calendar represents a time after the time represented by the specified Object.

booleanbefore(Object when)

Returns whether this Calendar represents a time before the time represented by the specified Object.

voidclear()

Sets all the calendar field values and the time value (millisecond offset from the Epoch) of this Calendar undefined.

voidclear(int field)

Sets the given calendar field value and the time value (millisecond offset from the Epoch) of this Calendar undefined.

Objectclone()

Creates and returns a copy of this object.

intcompareTo(Calendar anotherCalendar)

Compares the time values (millisecond offsets from the Epoch) represented by two Calendar objects.

protected voidcomplete()

Fills in any unset fields in the calendar fields.

protected abstract voidcomputeFields()

Converts the current millisecond time value time to calendar field values in fields[].

protected abstract voidcomputeTime()

Converts the current calendar field values in fields[] to the millisecond time value time.

booleanequals(Object obj)

Compares this Calendar to the specified Object.

intget(int field)

Returns the value of the given calendar field.

intgetActualMaximum(int field)

Returns the maximum value that the specified calendar field could have, given the time value of this Calendar.

intgetActualMinimum(int field)

Returns the minimum value that the specified calendar field could have, given the time value of this Calendar.

static Set<String>getAvailableCalendarTypes()

Returns an unmodifiable Set containing all calendar types supported by Calendar in the runtime environment.

static Locale[]getAvailableLocales()

Returns an array of all locales for which the getInstance methods of this class can return localized instances.

StringgetCalendarType()

Returns the calendar type of this Calendar.

StringgetDisplayName(int field, int style, Locale locale)

Returns the string representation of the calendar field value in the given style and locale.

Map<String,Integer>getDisplayNames(int field, int style, Locale locale)

Returns a Map containing all names of the calendar field in the given style and locale and their corresponding field values.

intgetFirstDayOfWeek()

Gets what the first day of the week is; e.g., SUNDAY in the U.S., MONDAY in France.

abstract intgetGreatestMinimum(int field)

Returns the highest minimum value for the given calendar field of this Calendar instance.

static CalendargetInstance()

Gets a calendar using the default time zone and locale.

static CalendargetInstance(Locale aLocale)

Gets a calendar using the default time zone and specified locale.

static CalendargetInstance(TimeZone zone)

Gets a calendar using the specified time zone and default locale.

static CalendargetInstance(TimeZone zone, Locale aLocale)

Gets a calendar with the specified time zone and locale.

abstract intgetLeastMaximum(int field)

Returns the lowest maximum value for the given calendar field of this Calendar instance.

abstract intgetMaximum(int field)

Returns the maximum value for the given calendar field of this Calendar instance.

intgetMinimalDaysInFirstWeek()

Gets what the minimal days required in the first week of the year are; e.g., if the first week is defined as one that contains the first day of the first month of a year, this method returns 1.

abstract intgetMinimum(int field)

Returns the minimum value for the given calendar field of this Calendar instance.

DategetTime()

Returns a Date object representing this Calendar's time value (millisecond offset from the Epoch").

longgetTimeInMillis()

Returns this Calendar's time value in milliseconds.

TimeZonegetTimeZone()

Gets the time zone.

intgetWeeksInWeekYear()

Returns the number of weeks in the week year represented by this Calendar.

intgetWeekYear()

Returns the week year represented by this Calendar.

inthashCode()

Returns a hash code for this calendar.

protected intinternalGet(int field)

Returns the value of the given calendar field.

booleanisLenient()

Tells whether date/time interpretation is to be lenient.

booleanisSet(int field)

Determines if the given calendar field has a value set, including cases that the value has been set by internal fields calculations triggered by a get method call.

booleanisWeekDateSupported()

Returns whether this Calendar supports week dates.

abstract voidroll(int field, boolean up)

Adds or subtracts (up/down) a single unit of time on the given time field without changing larger fields.

voidroll(int field, int amount)

Adds the specified (signed) amount to the specified calendar field without changing larger fields.

voidset(int field, int value)

Sets the given calendar field to the given value.

voidset(int year, int month, int date)

Sets the values for the calendar fields YEARMONTH, and DAY_OF_MONTH.

voidset(int year, int month, int date, int hourOfDay, int minute)

Sets the values for the calendar fields YEARMONTHDAY_OF_MONTHHOUR_OF_DAY, and MINUTE.

voidset(int year, int month, int date, int hourOfDay, int minute, int second)

Sets the values for the fields YEARMONTHDAY_OF_MONTHHOUR_OF_DAYMINUTE, and SECOND.

voidsetFirstDayOfWeek(int value)

Sets what the first day of the week is; e.g., SUNDAY in the U.S., MONDAY in France.

voidsetLenient(boolean lenient)

Specifies whether or not date/time interpretation is to be lenient.

voidsetMinimalDaysInFirstWeek(int value)

Sets what the minimal days required in the first week of the year are; For example, if the first week is defined as one that contains the first day of the first month of a year, call this method with value 1.

voidsetTime(Date date)

Sets this Calendar's time with the given Date.

voidsetTimeInMillis(long millis)

Sets this Calendar's current time from the given long value.

voidsetTimeZone(TimeZone value)

Sets the time zone with the given time zone value.

voidsetWeekDate(int weekYear, int weekOfYear, int dayOfWeek)

Sets the date of this Calendar with the the given date specifiers - week year, week of year, and day of week.

InstanttoInstant()

Converts this object to an Instant.

StringtoString()

Return a string representation of this calendar.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.hexiang.utils; import java.text.SimpleDateFormat; import java.util.*; public class CalendarUtil { public static void main(String args[]) { System.out.println("First day of week is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getFirstDateByWeek(new Date()))); System.out.println("Last day of week is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getLastDateByWeek(new Date()))); System.out.println("First day of month is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getFirstDateByMonth(new Date()))); System.out.println("Last day of month is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getLastDateByMonth(new Date()))); } /** * 获得所在星期的第一天 */ public static Date getFirstDateByWeek(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); int today = now.get(Calendar.DAY_OF_WEEK); int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一 now.set(Calendar.DATE, first_day_of_week); return now.getTime(); } /** * 获得所在星期的最后一天 */ public static Date getLastDateByWeek(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); int today = now.get(Calendar.DAY_OF_WEEK); int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一 int last_day_of_week = first_day_of_week + 6; // 星期日 now.set(Calendar.DATE, last_day_of_week); return now.getTime(); } /** * 获得所在月份的最后一天 * @param 当前月份所在的时间 * @return 月份的最后一天 */ public static Date getLastDateByMonth(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); now.set(Calendar.MONTH, now.get(Calendar.MONTH) + 1); now.set(Calendar.DATE, 1); now.set(Calendar.DATE, now.get(Calendar.DATE) - 1); now.set(Calendar.HOUR, 11); now.set(Calendar.MINUTE, 59); now.set(Calendar.SECOND, 59); return now.getTime(); } /** * 获得所在月份的第一天 * @param 当前月份所在的时间 * @return 月份的第一天 */ public static Date getFirstDateByMonth(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); now.set(Calendar.DATE, 0); now.set(Calendar.HOUR, 12); now.set(Calendar.MINUTE, 0); now.set(Calendar.SECOND, 0); return now.getTime(); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值