`
- 浏览:
80497 次
- 性别:
- 来自:
湖南
-
- <PRE class=java name="code">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;
-
-
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("获取当前月的第几周:" + 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.getInstance().get(Calendar.YEAR);
- }
-
-
-
public static int getMonth() {
-
return Calendar.getInstance().get(Calendar.MONTH) + 1;
- }
-
-
-
public static int getDayOfYear() {
-
return Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
- }
-
-
-
public static int getDayOfMonth() {
-
return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
- }
-
-
-
public static int getDayOfWeek() {
-
return Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
- }
-
-
-
public static int getWeekOfMonth() {
-
return Calendar.getInstance().get(Calendar.DAY_OF_WEEK_IN_MONTH);
- }
-
-
-
public static Date getTimeYearNext() {
-
Calendar.getInstance().add(Calendar.DAY_OF_YEAR, 183);
-
return Calendar.getInstance().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;
-
- }
-
-
-
private int getLastDayOfMonth(int year, int month) {
-
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
-
|| month == 10 || month == 12) {
-
return 31;
- }
-
if (month == 4 || month == 6 || month == 9 || month == 11) {
-
return 30;
- }
-
if (month == 2) {
-
if (isLeapYear(year)) {
-
return 29;
-
} else {
-
return 28;
- }
- }
-
return 0;
- }
-
-
-
public boolean isLeapYear(int year) {
-
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
- }
-
-
-
public boolean isLeapYear2(int year) {
-
return new GregorianCalendar().isLeapYear(year);
- }
- }
- </PRE>
-
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
7. **字符串常用方法**:Android中,`String`类提供了很多实用的方法,如`substring()`、`trim()`、`indexOf()`等,用于字符串的处理。此外,`TextUtils`类提供了更多的字符串操作,如空判断、格式化等。 8. **系统...
JAVA中常用类的常用方法主要涵盖了java语言中基础类库的关键类以及它们提供的主要方法。以下是针对文档内容的详细解释: 1. java.lang.Object类 Object类是Java中所有类的超类,它提供了多种方法,这些方法在Java中...
Java为每种基本数据类型都提供了对应的包装类,这些包装类不仅封装了基本类型的值,还提供了许多有用的静态方法。 1. **valueOf() 方法**:将字符串转换为相应类型的对象。 2. **parseInt() 方法**:将字符串转换为...
以下将详细讲解标题和描述中提到的几个关键工具类及其封装方法: 1. **DateUtil**: Java中的日期时间处理在早期版本中较为复杂,`DateUtil` 类通常是为了封装`java.util.Date`、`Calendar` 和 `java.time` 包中的类...
在Java编程语言中,封装是一种核心的面向对象编程原则,它允许我们将数据和操作这些数据的方法捆绑在一起,以提供更好的代码组织和安全性。标题提到的"封装java常用工具的类"是一个很好的实践,将常见的功能模块化,...
总的来说,"iOS工具类封装"中的`Helper`类集成了日常开发中常用的一些功能,提高了代码的可读性和效率。它简化了字符串尺寸计算、日期获取和数据验证的过程,为iOS开发者提供了便利。这样的工具类在大型项目中尤其有...
本压缩包"基于Android系统Api封装常用工具类.zip"中包含的AJT-code很可能是作者为了方便Android应用开发而编写的自定义工具类集合。 首先,我们要理解Android系统的API是什么。Android API是Google为开发者提供的...
实验旨在加深学生对Java预定义类的理解,掌握常用类的运用技巧,以及熟练方法覆盖(重写)的实践。实验分为三个部分,分别围绕Math类、基本类型的包装类、日期类(Date和Calendar)、以及动态数组Vector类展开。 ##...
包装类是 Java 语言中最基本的类之一,它将基本数据类型封装为对象。每个基本数据类型都有对应的包装类,例如 Boolean 对应 boolean、Byte 对应 byte、Short 对应 short、Integer 对应 int、Long 对应 long、...
在这个“Java8应用封装,手写ORM,LOG,framework”的项目中,开发者显然专注于利用Java8的新特性来构建自己的轻量级框架,包括对象关系映射(ORM)、日志系统(LOG)以及一些通用的工具类和自动化配置。 1. **Java...
这些包装类不仅提供了基本类型的值封装,还提供了一些有用的静态方法,如`parseInt(String s)`用于将字符串转换为整数。 综上所述,理解`Object`类的核心方法(如`toString`和`equals`)及其重写方式对于提高代码...
1. **java.util.*** 包:这是Java最基础的工具类包,包含了集合框架(如ArrayList、LinkedList、HashMap等)、日期时间处理(Date、Calendar、SimpleDateFormat)、泛型、迭代器、枚举等重要工具类。例如,`...
思科路由器常用配置命令大全 ...Encapsulation PPP命令把PPP设置为由串口或ISDN接口使用的封装方法。Encapsulation sap命令规定在网络段上使用的以太网802.2格式Cisco的密码是sap。 二十三、End命令 End命令退出。
总的来说,这些Java常用类为开发者提供了丰富的工具,涵盖了对象基础、字符串处理、线程控制、数据封装和日期时间操作等多个方面,是Java编程不可或缺的部分。理解和熟练使用这些类,能够大大提高开发效率和代码质量...
常用方法1.1.2.字符串比较1.1.3. StringBuffer与 StringBuilder1.1.4. String、StringBuffer、StringBuilder的区别1.1.5.封装类比较1.2. Math类1.3. Arrays 类1.4. System 类1.5. Object 类1.6. Date 类1.6.1.日期...
工具类可能包含对旧版`java.util.Date`和`java.util.Calendar`的封装,或者使用新API的实用方法。 3. **字符串操作**:字符串在编程中非常常见,工具类可能会提供一些便捷的字符串格式化、拼接、查找、替换、分割等...
* 在NetBeans开发环境中阅读并分析程序,完成验证性实验,理解异常处理的方法,掌握String类的常用方法,掌握Date类以及Calendar类的常用方法,掌握BigInteger类的常用方法 * 依据题目要求设计程序,练习基本的算法...
基本类型是Java语言的基本数据类型,例如int、float、double等,而包装类则是对基本类型的封装,例如Integer、Float、Double等。包装类提供了许多有用的方法,可以对基本类型进行操作。 3. Runtime类 Runtime类是...