`
- 浏览:
239240 次
- 性别:
- 来自:
北京
-
-
package
com.iwode.common;
-
-
import
java.text.DateFormat;
-
import
java.text.ParsePosition;
-
import
java.text.SimpleDateFormat;
-
import
java.util.Calendar;
-
import
java.util.Date;
-
import
java.util.GregorianCalendar;
-
-
-
-
-
-
-
public
class
CalendarUtil {
-
-
-
private
int
weeks =
0
;
-
private
int
MaxDate;
-
private
int
MaxYear;
-
private
static
Calendar calendar = Calendar.getInstance();
-
-
-
-
-
public
static
void
main(String[] args) {
-
CalendarUtil tt = new
CalendarUtil();
-
System.out.println("获取当天日期:"
+ tt.getNowTime(
"yyyy-MM-dd"
));
-
System.out.println("获取本周一日期:"
+ tt.getMondayOFWeek());
-
System.out.println("获取本周日的日期~:"
+ tt.getCurrentWeekday());
-
System.out.println("获取上周一日期:"
+ tt.getPreviousWeekday());
-
System.out.println("获取上周日日期:"
+ tt.getPreviousWeekSunday());
-
System.out.println("获取下周一日期:"
+ tt.getNextMonday());
-
System.out.println("获取下周日日期:"
+ tt.getNextSunday());
-
System.out.println("获得相应周的周六的日期:"
+ tt.getNowTime(
"yyyy-MM-dd"
));
-
System.out.println("获取本月第一天日期:"
+ tt.getFirstDayOfMonth());
-
System.out.println("获取本月最后一天日期:"
+ tt.getDefaultDay());
-
System.out.println("获取上月第一天日期:"
+ tt.getPreviousMonthFirst());
-
System.out.println("获取上月最后一天的日期:"
+ tt.getPreviousMonthEnd());
-
System.out.println("获取下月第一天日期:"
+ tt.getNextMonthFirst());
-
System.out.println("获取下月最后一天日期:"
+ tt.getNextMonthEnd());
-
System.out.println("获取本年的第一天日期:"
+ tt.getCurrentYearFirst());
-
System.out.println("获取本年最后一天日期:"
+ tt.getCurrentYearEnd());
-
System.out.println("获取去年的第一天日期:"
+ tt.getPreviousYearFirst());
-
System.out.println("获取去年的最后一天日期:"
+ tt.getPreviousYearEnd());
-
System.out.println("获取明年第一天日期:"
+ tt.getNextYearFirst());
-
System.out.println("获取明年最后一天日期:"
+ tt.getNextYearEnd());
-
System.out.println("获取本季度第一天:"
+ tt.getThisSeasonFirstTime(
11
));
-
System.out.println("获取本季度最后一天:"
+ tt.getThisSeasonFinallyTime(
11
));
-
System.out.println("获取两个日期之间间隔天数2008-12-1~2008-9.29:"
-
+ CalendarUtil.getTwoDay("2008-12-1"
,
"2008-9-29"
));
-
System.out.println("---------------->"
);
-
System.out.println("获取当前月的第几周:"
+ tt.getWeekOfMonth());
-
System.out.println("获取当前年份:"
+ tt.getYear());
-
System.out.println("获取当前月份:"
+ tt.getMonth());
-
System.out.println("获取今天在本年的第几天:"
+ tt.getDayOfYear());
-
-
System.out.println("获得今天在本月的第几天(获得当前日):"
+ tt.getDayOfMonth());
-
System.out.println("获得今天在本周的第几天:"
+ tt.getDayOfWeek());
-
System.out.println("获得半年后的日期:"
+ tt.convertDateToString(tt.getTimeYearNext()));
-
}
-
-
-
-
-
-
-
public
static
int
getYear() {
-
return
calendar.get(Calendar.YEAR);
-
}
-
-
-
-
-
-
-
public
static
int
getMonth() {
-
return
calendar.get(Calendar.MONTH) +
1
;
-
}
-
-
-
-
-
-
-
public
static
int
getDayOfYear() {
-
return
calendar.get(Calendar.DAY_OF_YEAR);
-
}
-
-
-
-
-
-
-
public
static
int
getDayOfMonth() {
-
return
calendar.get(Calendar.DAY_OF_MONTH);
-
}
-
-
-
-
-
-
-
public
static
int
getDayOfWeek() {
-
return
calendar.get(Calendar.DAY_OF_WEEK);
-
}
-
-
-
-
-
-
-
public
static
int
getWeekOfMonth() {
-
return
calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH);
-
}
-
-
-
-
-
-
-
public
static
Date getTimeYearNext() {
-
calendar.add(Calendar.DAY_OF_YEAR, 183
);
-
return
calendar.getTime();
-
}
-
-
public
static
String convertDateToString(Date dateTime){
-
SimpleDateFormat df=new
SimpleDateFormat(
"yyyy-MM-dd"
);
-
return
df.format(dateTime);
-
}
-
-
-
-
-
public
static
String getTwoDay(String sj1, String sj2) {
-
SimpleDateFormat myFormatter = new
SimpleDateFormat(
"yyyy-MM-dd"
);
-
long
day =
0
;
-
try
{
-
java.util.Date date = myFormatter.parse(sj1);
-
java.util.Date mydate = myFormatter.parse(sj2);
-
day = (date.getTime() - mydate.getTime()) / (24
*
60
*
60
*
1000
);
-
} catch
(Exception e) {
-
return
""
;
-
}
-
return
day +
""
;
-
}
-
-
-
-
-
-
-
-
public
static
String getWeek(String sdate) {
-
-
Date date = CalendarUtil.strToDate(sdate);
-
Calendar c = Calendar.getInstance();
-
c.setTime(date);
-
-
-
-
return
new
SimpleDateFormat(
"EEEE"
).format(c.getTime());
-
}
-
-
-
-
-
-
-
-
public
static
Date strToDate(String strDate) {
-
SimpleDateFormat formatter = new
SimpleDateFormat(
"yyyy-MM-dd"
);
-
ParsePosition pos = new
ParsePosition(
0
);
-
Date strtodate = formatter.parse(strDate, pos);
-
return
strtodate;
-
}
-
-
-
-
-
-
-
-
-
public
static
long
getDays(String date1, String date2) {
-
if
(date1 ==
null
|| date1.equals(
""
))
-
return
0
;
-
if
(date2 ==
null
|| date2.equals(
""
))
-
return
0
;
-
-
SimpleDateFormat myFormatter = new
SimpleDateFormat(
"yyyy-MM-dd"
);
-
java.util.Date date = null
;
-
java.util.Date mydate = null
;
-
try
{
-
date = myFormatter.parse(date1);
-
mydate = myFormatter.parse(date2);
-
} catch
(Exception e) {
-
}
-
long
day = (date.getTime() - mydate.getTime()) / (
24
*
60
*
60
*
1000
);
-
return
day;
-
}
-
-
-
public
String getDefaultDay() {
-
String str = ""
;
-
SimpleDateFormat sdf = new
SimpleDateFormat(
"yyyy-MM-dd"
);
-
-
Calendar lastDate = Calendar.getInstance();
-
lastDate.set(Calendar.DATE, 1
);
-
lastDate.add(Calendar.MONTH, 1
);
-
lastDate.add(Calendar.DATE, -1
);
-
-
str = sdf.format(lastDate.getTime());
-
return
str;
-
}
-
-
-
public
String getPreviousMonthFirst() {
-
String str = ""
;
-
SimpleDateFormat sdf = new
SimpleDateFormat(
"yyyy-MM-dd"
);
-
-
Calendar lastDate = Calendar.getInstance();
-
lastDate.set(Calendar.DATE, 1
);
-
lastDate.add(Calendar.MONTH, -1
);
-
-
-
str = sdf.format(lastDate.getTime());
-
return
str;
-
}
-
-
-
public
String getFirstDayOfMonth() {
-
String str = ""
;
-
SimpleDateFormat sdf = new
SimpleDateFormat(
"yyyy-MM-dd"
);
-
-
Calendar lastDate = Calendar.getInstance();
-
lastDate.set(Calendar.DATE, 1
);
-
str = sdf.format(lastDate.getTime());
-
return
str;
-
}
-
-
-
public
String getCurrentWeekday() {
-
weeks = 0
;
-
int
mondayPlus =
this
.getMondayPlus();
-
GregorianCalendar currentDate = new
GregorianCalendar();
-
currentDate.add(GregorianCalendar.DATE, mondayPlus + 6
);
-
Date monday = currentDate.getTime();
-
-
DateFormat df = DateFormat.getDateInstance();
-
String preMonday = df.format(monday);
-
return
preMonday;
-
}
-
-
-
public
String getNowTime(String dateformat) {
-
Date now = new
Date();
-
SimpleDateFormat dateFormat = new
SimpleDateFormat(dateformat);
-
String hehe = dateFormat.format(now);
-
return
hehe;
-
}
-
-
-
private
int
getMondayPlus() {
-
Calendar cd = Calendar.getInstance();
-
-
int
dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) -
1
;
-
if
(dayOfWeek ==
1
) {
-
return
0
;
-
} else
{
-
return
1
- dayOfWeek;
-
}
-
}
-
-
-
public
String getMondayOFWeek() {
-
weeks = 0
;
-
int
mondayPlus =
this
.getMondayPlus();
-
GregorianCalendar currentDate = new
GregorianCalendar();
-
currentDate.add(GregorianCalendar.DATE, mondayPlus);
-
Date monday = currentDate.getTime();
-
-
DateFormat df = DateFormat.getDateInstance();
-
String preMonday = df.format(monday);
-
return
preMonday;
-
}
-
-
-
public
String getSaturday() {
-
int
mondayPlus =
this
.getMondayPlus();
-
GregorianCalendar currentDate = new
GregorianCalendar();
-
currentDate.add(GregorianCalendar.DATE, mondayPlus + 7
* weeks +
6
);
-
Date monday = currentDate.getTime();
-
DateFormat df = DateFormat.getDateInstance();
-
String preMonday = df.format(monday);
-
return
preMonday;
-
}
-
-
-
public
String getPreviousWeekSunday() {
-
weeks = 0
;
-
weeks--;
-
int
mondayPlus =
this
.getMondayPlus();
-
GregorianCalendar currentDate = new
GregorianCalendar();
-
currentDate.add(GregorianCalendar.DATE, mondayPlus + weeks);
-
Date monday = currentDate.getTime();
-
DateFormat df = DateFormat.getDateInstance();
-
String preMonday = df.format(monday);
-
return
preMonday;
-
}
-
-
-
public
String getPreviousWeekday() {
-
weeks--;
-
int
mondayPlus =
this
.getMondayPlus();
-
GregorianCalendar currentDate = new
GregorianCalendar();
-
currentDate.add(GregorianCalendar.DATE, mondayPlus + 7
* weeks);
-
Date monday = currentDate.getTime();
-
DateFormat df = DateFormat.getDateInstance();
-
String preMonday = df.format(monday);
-
return
preMonday;
-
}
-
-
-
public
String getNextMonday() {
-
weeks++;
-
int
mondayPlus =
this
.getMondayPlus();
-
GregorianCalendar currentDate = new
GregorianCalendar();
-
currentDate.add(GregorianCalendar.DATE, mondayPlus + 7
);
-
Date monday = currentDate.getTime();
-
DateFormat df = DateFormat.getDateInstance();
-
String preMonday = df.format(monday);
-
return
preMonday;
-
}
-
-
-
public
String getNextSunday() {
-
-
int
mondayPlus =
this
.getMondayPlus();
-
GregorianCalendar currentDate = new
GregorianCalendar();
-
currentDate.add(GregorianCalendar.DATE, mondayPlus + 7
+
6
);
-
Date monday = currentDate.getTime();
-
DateFormat df = DateFormat.getDateInstance();
-
String preMonday = df.format(monday);
-
return
preMonday;
-
}
-
-
private
int
getMonthPlus() {
-
Calendar cd = Calendar.getInstance();
-
int
monthOfNumber = cd.get(Calendar.DAY_OF_MONTH);
-
cd.set(Calendar.DATE, 1
);
-
cd.roll(Calendar.DATE, -1
);
-
MaxDate = cd.get(Calendar.DATE);
-
if
(monthOfNumber ==
1
) {
-
return
-MaxDate;
-
} else
{
-
return
1
- monthOfNumber;
-
}
-
}
-
-
-
public
String getPreviousMonthEnd() {
-
String str = ""
;
-
SimpleDateFormat sdf = new
SimpleDateFormat(
"yyyy-MM-dd"
);
-
-
Calendar lastDate = Calendar.getInstance();
-
lastDate.add(Calendar.MONTH, -1
);
-
lastDate.set(Calendar.DATE, 1
);
-
lastDate.roll(Calendar.DATE, -1
);
-
str = sdf.format(lastDate.getTime());
-
return
str;
-
}
-
-
-
public
String getNextMonthFirst() {
-
String str = ""
;
-
SimpleDateFormat sdf = new
SimpleDateFormat(
"yyyy-MM-dd"
);
-
-
Calendar lastDate = Calendar.getInstance();
-
lastDate.add(Calendar.MONTH, 1
);
-
lastDate.set(Calendar.DATE, 1
);
-
str = sdf.format(lastDate.getTime());
-
return
str;
-
}
-
-
-
public
String getNextMonthEnd() {
-
String str = ""
;
-
SimpleDateFormat sdf = new
SimpleDateFormat(
"yyyy-MM-dd"
);
-
-
Calendar lastDate = Calendar.getInstance();
-
lastDate.add(Calendar.MONTH, 1
);
-
lastDate.set(Calendar.DATE, 1
);
-
lastDate.roll(Calendar.DATE, -1
);
-
str = sdf.format(lastDate.getTime());
-
return
str;
-
}
-
-
-
public
String getNextYearEnd() {
-
String str = ""
;
-
SimpleDateFormat sdf = new
SimpleDateFormat(
"yyyy-MM-dd"
);
-
-
Calendar lastDate = Calendar.getInstance();
-
lastDate.add(Calendar.YEAR, 1
);
-
lastDate.set(Calendar.DAY_OF_YEAR, 1
);
-
lastDate.roll(Calendar.DAY_OF_YEAR, -1
);
-
str = sdf.format(lastDate.getTime());
-
return
str;
-
}
-
-
-
public
String getNextYearFirst() {
-
String str = ""
;
-
SimpleDateFormat sdf = new
SimpleDateFormat(
"yyyy-MM-dd"
);
-
-
Calendar lastDate = Calendar.getInstance();
-
lastDate.add(Calendar.YEAR, 1
);
-
lastDate.set(Calendar.DAY_OF_YEAR, 1
);
-
str = sdf.format(lastDate.getTime());
-
return
str;
-
-
}
-
-
-
private
int
getMaxYear() {
-
Calendar cd = Calendar.getInstance();
-
cd.set(Calendar.DAY_OF_YEAR, 1
);
-
cd.roll(Calendar.DAY_OF_YEAR, -1
);
-
int
MaxYear = cd.get(Calendar.DAY_OF_YEAR);
-
return
MaxYear;
-
}
-
-
private
int
getYearPlus() {
-
Calendar cd = Calendar.getInstance();
-
int
yearOfNumber = cd.get(Calendar.DAY_OF_YEAR);
-
cd.set(Calendar.DAY_OF_YEAR, 1
);
-
cd.roll(Calendar.DAY_OF_YEAR, -1
);
-
int
MaxYear = cd.get(Calendar.DAY_OF_YEAR);
-
if
(yearOfNumber ==
1
) {
-
return
-MaxYear;
-
} else
{
-
return
1
- yearOfNumber;
-
}
-
}
-
-
-
public
String getCurrentYearFirst() {
-
int
yearPlus =
this
.getYearPlus();
-
GregorianCalendar currentDate = new
GregorianCalendar();
-
currentDate.add(GregorianCalendar.DATE, yearPlus);
-
Date yearDay = currentDate.getTime();
-
DateFormat df = DateFormat.getDateInstance();
-
String preYearDay = df.format(yearDay);
-
return
preYearDay;
-
}
-
-
-
public
String getCurrentYearEnd() {
-
Date date = new
Date();
-
SimpleDateFormat dateFormat = new
SimpleDateFormat(
"yyyy"
);
-
String years = dateFormat.format(date);
-
return
years +
"-12-31"
;
-
}
-
-
-
public
String getPreviousYearFirst() {
-
Date date = new
Date();
-
SimpleDateFormat dateFormat = new
SimpleDateFormat(
"yyyy"
);
-
String years = dateFormat.format(date);
-
int
years_value = Integer.parseInt(years);
-
years_value--;
-
return
years_value +
"-1-1"
;
-
}
-
-
-
public
String getPreviousYearEnd() {
-
weeks--;
-
int
yearPlus =
this
.getYearPlus();
-
GregorianCalendar currentDate = new
GregorianCalendar();
-
currentDate.add(GregorianCalendar.DATE, yearPlus + MaxYear * weeks
-
+ (MaxYear - 1
));
-
Date yearDay = currentDate.getTime();
-
DateFormat df = DateFormat.getDateInstance();
-
String preYearDay = df.format(yearDay);
-
return
preYearDay;
-
}
-
-
-
public
String getThisSeasonFirstTime(
int
month) {
-
int
array[][] = { {
1
,
2
,
3
}, {
4
,
5
,
6
}, {
7
,
8
,
9
}, {
10
,
11
,
12
} };
-
int
season =
1
;
-
if
(month >=
1
&& month <=
3
) {
-
season = 1
;
-
}
-
if
(month >=
4
&& month <=
6
) {
-
season = 2
;
-
}
-
if
(month >=
7
&& month <=
9
) {
-
season = 3
;
-
}
-
if
(month >=
10
&& month <=
12
) {
-
season = 4
;
-
}
-
int
start_month = array[season -
1
][
0
];
-
int
end_month = array[season -
1
][
2
];
-
-
Date date = new
Date();
-
SimpleDateFormat dateFormat = new
SimpleDateFormat(
"yyyy"
);
-
String years = dateFormat.format(date);
-
int
years_value = Integer.parseInt(years);
-
-
int
start_days =
1
;
-
int
end_days = getLastDayOfMonth(years_value, end_month);
-
String seasonDate = years_value + "-"
+ start_month +
"-"
+ start_days;
-
return
seasonDate;
-
-
}
-
-
-
public
String getThisSeasonFinallyTime(
int
month) {
-
int
array[][] = { {
1
,
2
,
3
}, {
4
,
5
,
6
}, {
7
,
8
,
9
}, {
10
,
11
,
12
} };
-
int
season =
1
;
-
if
(month >=
1
&& month <=
3
) {
-
season = 1
;
-
}
-
if
(month >=
4
&& month <=
6
) {
-
season = 2
;
-
}
-
if
(month >=
7
&& month <=
9
) {
-
season = 3
;
-
}
-
if
(month >=
10
&& month <=
12
) {
-
season = 4
;
-
}
-
int
start_month = array[season -
1
][
0
];
-
int
end_month = array[season -
1
][
2
];
-
-
Date date = new
Date();
-
SimpleDateFormat dateFormat = new
SimpleDateFormat(
"yyyy"
);
-
String years = dateFormat.format(date);
-
int
years_value = Integer.parseInt(years);
-
-
int
start_days =
1
;
-
int
end_days = getLastDayOfMonth(years_value, end_month);
-
String seasonDate = years_value + "-"
+ end_month +
"-"
+ end_days;
-
return
seasonDate;
-
-
}
-
-
-
-
-
-
-
-
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
本教程将详细探讨如何封装一个基于UIPickerView的日历控件,以实现自定义UI效果并根据不同类型加载不同数据。 首先,让我们了解UIPickerView的基本概念。UIPickerView是iOS SDK提供的一种组件,它允许用户在滚动...
这个"php封装的日历类.zip"文件提供了一个已经封装好的PHP类,可以帮助开发者便捷地实现日历功能。下面我们将深入探讨这个日历类的设计和使用方法。 首先,这个类库的核心是创建一个类,可能命名为`Calendar`,它...
自己写的日历类,连样式表都封装了,只要几句话就可以调用.
"GXT自己封装的Calendar日历类"就是这样的一个自定义组件,它提供了更丰富的功能和更好的用户体验,相比GWT原生的日期选择器。GXT(Ext GWT)是Sencha公司为GWT提供的一套强大的UI库,它扩展了GWT的功能,提供了许多...
"基于CollectionView的日历封装"这个项目就是利用UICollectionView的特性,为开发者提供一个易于使用、高度可定制的日历组件。下面我们将深入探讨这个话题。 首先,`CalendarView.h`和`CalendarView.m`是Objective-...
本资源提供了一个用JS封装好的日历代码,这是一个非常实用的功能,通常用于网站上的日期选择器或者事件管理应用。下面我们将详细探讨这个日历代码的实现原理、关键功能以及可能的扩展点。 首先,让我们了解日历组件...
"ios-封装日历控件.zip" 提供了一个预封装的日历组件,名为HQCalendar,适用于快速构建支持时间选择功能的应用。以下是关于这个日历控件以及iOS中日历编程的一些关键知识点: 1. **自定义控件封装**: 在iOS开发中...
Java中的日历工具类是Java开发中不...这个“实用的日历工具类”可能就是对这些工具的封装,以提供更加便捷的使用体验。通过理解和掌握这些API,你可以更高效地处理日期和时间相关的问题,提高代码的可读性和可维护性。
- 日程数据通常需要封装在一个自定义类中,包含日程的开始时间、结束时间、标题、描述等属性。这些数据结构可以与后台数据库或网络服务进行交互,以实现日程的添加、删除和同步。 5. **适配器(Adapter)**: - ...
- 创建一个新的Fragment类(例如:CustomCalendarFragment),在其中持有日历视图和Adapter实例。 - 在onCreateView方法中,加载布局并初始化日历控件,设置Adapter。 - 提供公共方法来设置初始选中日期、获取...
最后,为了让日历组件更易于使用和复用,可以将其封装成一个可配置的函数或类,接收参数如当前日期、起始星期等,返回HTML结构或者操作DOM的方法。 总的来说,实现一个JavaScript日历涉及到JavaScript的日期处理、...
在Java编程中,这些类可能遵循了面向对象的设计原则,如封装、继承和多态。例如,Month 和 Year 可能都继承自一个更抽象的 CalendarComponent 类,它们共享一些通用的属性和方法。同时,这些类也可能使用了设计模式...
如果日历控件代码量较大,可采用模块化设计,如ES6的`class`语法定义一个日历类,封装相关方法和属性。这样便于代码维护和复用,同时避免全局变量污染。 8. **插件和库** 开源社区提供了很多现成的日历插件和库,...
标题中的“一个vb写的日历类”指的是使用Visual Basic(VB)编程语言开发的一个日历控件或类库。Visual Basic是微软推出的一种面向对象的编程工具,特别适合于快速构建Windows应用程序。在这个项目中,开发者创建了...
在本案例中,我们讨论的是一个自定义封装的C++日历类,该类不仅提供基本的日历功能,还增加了时间数据的补充功能,比如将"2012-4-2"这样的日期格式转换为更标准的"2012-04-02"格式。 首先,我们要理解C++中的时间...
1. **面向对象编程**:C++支持类和对象的概念,通过封装、继承和多态性来组织代码。在这个项目中,日历、日期、时间等可能会被定义为类,每个类都有相应的属性和方法。 2. **Windows API或MFC**:在Windows平台上...
这些函数可能封装在名为`CalendarModel`的类中,负责日历的逻辑运算。 5. **用户交互**:UGUI允许通过事件系统(EventSystem)来处理用户输入,比如点击日历上的日期单元格。在Demo中,可能会有事件监听器(Event...
5. **模块化与封装**:为了提高代码的可复用性和维护性,我们需要将日历功能封装为一个独立的模块,可能是一个函数或者一个类。这样在其他项目中可以直接引入这个模块,快速实现日历功能。 6. **兼容性考虑**:由于...
通过这个日历类,初学者可以学习如何组织代码,理解类与对象之间的关系,以及如何将复杂问题分解为可管理的部分。此外,这也可以帮助他们熟悉PHP的OOP特性,比如抽象类、接口和命名空间,这些是构建大型、可维护的...
MFC是Microsoft为简化Windows API编程而开发的一个类库,它封装了Windows API,使得开发者能够用C++编写出更面向对象的Windows应用程序。MFC包含了大量的类,这些类代表了Windows操作系统中的各种对象,如窗口、菜单...