日期获取方式
2016-03-23 11:07
323 查看
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
/**
* @author cxn
* 2016年3月23日
日期工具类
*/
public class DateUtil {
private static final int FIRST_DAY = Calendar.MONDAY;
// 获取本月第一天日期
public static String getTimesMonthmorning() {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
Date theDate = calendar.getTime();
GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
gcLast.setTime(theDate);
// 设置为第一天
gcLast.set(Calendar.DAY_OF_MONTH, 1);
String day_first = sf.format(gcLast.getTime());
return day_first;
}
// 获取本月最后一天日期
public static String getTimesMonthLast() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar ca = Calendar.getInstance();
ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
String last = format.format(ca.getTime());
return last;
}
// 获取本周的星期一的日期
public static String getTimesWeekfirst() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // 获取本周一的日期
return df.format(cal.getTime());
}
// 获取本周星期天的日期
public static String getTimesWeeklast() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
return df.format(cal.getTime());
}
// 计算目前是一年中的第几周
public static int week() {
Calendar cal = Calendar.getInstance(Locale.CHINA); // 实例化cal
cal.setFirstDayOfWeek(Calendar.MONDAY);
int week = cal.get(Calendar.WEEK_OF_YEAR);
return week;
}
// 计算今年的年份
public static int year() {
Calendar cal = Calendar.getInstance(Locale.CHINA); // 实例化cal
cal.setFirstDayOfWeek(Calendar.MONDAY);
int year = cal.get(Calendar.YEAR);
return year;
}
// 计算当前的月份
public static int month() {
Calendar cal = Calendar.getInstance(Locale.CHINA); // 实例化cal
cal.setFirstDayOfWeek(Calendar.MONDAY);
return cal.get(Calendar.MONTH) + 1;
}
// 获取本周第星期一的日期 date 类型
public static Date convertWeekByDate(Date time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式
Calendar cal = Calendar.getInstance();
cal.setTime(time);
// 判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
int day = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
String imptimeBegin = sdf.format(cal.getTime());
cal.add(Calendar.DATE, 6);
try {
Date date = sdf.parse(imptimeBegin);
return date;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
// 计算当前年的开始日期
public static String getCurrentYearStartTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
String now = null;
try {
c.set(Calendar.MONTH, 0);
c.set(Calendar.DATE, 1);
now = sdf.format(c.getTime());
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
// 计算当前年的结束日期
public static String getCurrentYearEndTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
String now = null;
try {
c.set(Calendar.MONTH, 11);
c.set(Calendar.DATE, 31);
now = sdf.format(c.getTime());
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
// 计算当前季度的开始日期
public static String getCurrentQuarterStartTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
int currentMonth = c.get(Calendar.MONTH) + 1;
String now = null;
try {
if (currentMonth >= 1 && currentMonth <= 3)
c.set(Calendar.MONTH, 0);
else if (currentMonth >= 4 && currentMonth <= 6)
c.set(Calendar.MONTH, 3);
else if (currentMonth >= 7 && currentMonth <= 9)
c.set(Calendar.MONTH, 4);
else if (currentMonth >= 10 && currentMonth <= 12)
c.set(Calendar.MONTH, 9);
c.set(Calendar.DATE, 1);
now = sdf.format(c.getTime());
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
//计算当前季度的結束日期
public static String getCurrentQuarterEndTime() {
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
int currentMonth = c.get(Calendar.MONTH) + 1;
String now = null;
try {
if (currentMonth >= 1 && currentMonth <= 3) {
c.set(Calendar.MONTH, 2);
c.set(Calendar.DATE, 31);
} else if (currentMonth >= 4 && currentMonth <= 6) {
c.set(Calendar.MONTH, 5);
c.set(Calendar.DATE, 30);
} else if (currentMonth >= 7 && currentMonth <= 9) {
c.set(Calendar.MONTH, 8);
c.set(Calendar.DATE, 30);
} else if (currentMonth >= 10 && currentMonth <= 12) {
c.set(Calendar.MONTH, 11);
c.set(Calendar.DATE, 31);
}
now = sdf.format(c.getTime());
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
// 获取任意一天的日期
public static Date getDate(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, 1);
Date date1 = new Date(calendar.getTimeInMillis());
return date1;
}
//獲取當前日期季度
public static Integer getCurrentQuarter() {
Calendar c = Calendar.getInstance();
int currentMonth = c.get(Calendar.MONTH) + 1;
Integer now = 0;
if (currentMonth >= 1 && currentMonth <= 3) {
now = 1;
} else if (currentMonth >= 4 && currentMonth <= 6) {
now = 2;
} else if (currentMonth >= 7 && currentMonth <= 9) {
now = 3;
} else if (currentMonth >= 10 && currentMonth <= 12) {
now = 4;
}
return now;
}
//數字日期和月份的轉化
public static String getMonth(Integer month) {
String mon = null;
if (month == 1) {
mon = "一月份";
} else if (month == 2) {
mon = "二月份";
} else if (month == 3) {
mon = "三月份";
} else if (month == 4) {
mon = "四月份";
} else if (month == 5) {
mon = "五月份";
} else if (month == 6) {
mon = "六月份";
} else if (month == 7) {
mon = "七月份";
} else if (month == 8) {
mon = "八月份";
} else if (month == 9) {
mon = "九月份";
} else if (month == 10) {
mon = "十月份";
} else if (month == 11) {
mon = "十一月份";
} else if (month == 12) {
mon = "十二月份";
}
return mon;
}
//計算本週的週一到周七的所有日期時間
public static List<String> getWeekdays() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
while (calendar.get(Calendar.DAY_OF_WEEK) != FIRST_DAY) {
calendar.add(Calendar.DATE, -1);
}
List<String> list = new ArrayList<String>();
// setToFirstDay(calendar);
for (int i = 0; i < 7; i++) {
// System.out.println(df.format(calendar.getTime()));
list.add(df.format(calendar.getTime()));
calendar.add(Calendar.DATE, 1);
}
return list;
}
//傳進去的d的日期的後6天的日期
public static String getlast(String d) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dt = (Date) sdf.parse(d, new ParsePosition(0));
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(dt);
rightNow.add(Calendar.DATE, 6);// 你要加减的日期
Date dt1 = (Date) rightNow.getTime();
String da = sdf.format(dt1);
return da;
}
// 计算2个日期之间相差的天数
public static int daysBetween(Date smdate, Date bdate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
smdate = sdf.parse(sdf.format(smdate));
bdate = sdf.parse(sdf.format(bdate));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
cal.setTime(smdate);
long time1 = cal.getTimeInMillis();
cal.setTime(bdate);
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(between_days));
}
}
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
/**
* @author cxn
* 2016年3月23日
日期工具类
*/
public class DateUtil {
private static final int FIRST_DAY = Calendar.MONDAY;
// 获取本月第一天日期
public static String getTimesMonthmorning() {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
Date theDate = calendar.getTime();
GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
gcLast.setTime(theDate);
// 设置为第一天
gcLast.set(Calendar.DAY_OF_MONTH, 1);
String day_first = sf.format(gcLast.getTime());
return day_first;
}
// 获取本月最后一天日期
public static String getTimesMonthLast() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar ca = Calendar.getInstance();
ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
String last = format.format(ca.getTime());
return last;
}
// 获取本周的星期一的日期
public static String getTimesWeekfirst() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // 获取本周一的日期
return df.format(cal.getTime());
}
// 获取本周星期天的日期
public static String getTimesWeeklast() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
return df.format(cal.getTime());
}
// 计算目前是一年中的第几周
public static int week() {
Calendar cal = Calendar.getInstance(Locale.CHINA); // 实例化cal
cal.setFirstDayOfWeek(Calendar.MONDAY);
int week = cal.get(Calendar.WEEK_OF_YEAR);
return week;
}
// 计算今年的年份
public static int year() {
Calendar cal = Calendar.getInstance(Locale.CHINA); // 实例化cal
cal.setFirstDayOfWeek(Calendar.MONDAY);
int year = cal.get(Calendar.YEAR);
return year;
}
// 计算当前的月份
public static int month() {
Calendar cal = Calendar.getInstance(Locale.CHINA); // 实例化cal
cal.setFirstDayOfWeek(Calendar.MONDAY);
return cal.get(Calendar.MONTH) + 1;
}
// 获取本周第星期一的日期 date 类型
public static Date convertWeekByDate(Date time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式
Calendar cal = Calendar.getInstance();
cal.setTime(time);
// 判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
int day = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
String imptimeBegin = sdf.format(cal.getTime());
cal.add(Calendar.DATE, 6);
try {
Date date = sdf.parse(imptimeBegin);
return date;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
// 计算当前年的开始日期
public static String getCurrentYearStartTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
String now = null;
try {
c.set(Calendar.MONTH, 0);
c.set(Calendar.DATE, 1);
now = sdf.format(c.getTime());
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
// 计算当前年的结束日期
public static String getCurrentYearEndTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
String now = null;
try {
c.set(Calendar.MONTH, 11);
c.set(Calendar.DATE, 31);
now = sdf.format(c.getTime());
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
// 计算当前季度的开始日期
public static String getCurrentQuarterStartTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
int currentMonth = c.get(Calendar.MONTH) + 1;
String now = null;
try {
if (currentMonth >= 1 && currentMonth <= 3)
c.set(Calendar.MONTH, 0);
else if (currentMonth >= 4 && currentMonth <= 6)
c.set(Calendar.MONTH, 3);
else if (currentMonth >= 7 && currentMonth <= 9)
c.set(Calendar.MONTH, 4);
else if (currentMonth >= 10 && currentMonth <= 12)
c.set(Calendar.MONTH, 9);
c.set(Calendar.DATE, 1);
now = sdf.format(c.getTime());
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
//计算当前季度的結束日期
public static String getCurrentQuarterEndTime() {
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
int currentMonth = c.get(Calendar.MONTH) + 1;
String now = null;
try {
if (currentMonth >= 1 && currentMonth <= 3) {
c.set(Calendar.MONTH, 2);
c.set(Calendar.DATE, 31);
} else if (currentMonth >= 4 && currentMonth <= 6) {
c.set(Calendar.MONTH, 5);
c.set(Calendar.DATE, 30);
} else if (currentMonth >= 7 && currentMonth <= 9) {
c.set(Calendar.MONTH, 8);
c.set(Calendar.DATE, 30);
} else if (currentMonth >= 10 && currentMonth <= 12) {
c.set(Calendar.MONTH, 11);
c.set(Calendar.DATE, 31);
}
now = sdf.format(c.getTime());
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
// 获取任意一天的日期
public static Date getDate(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, 1);
Date date1 = new Date(calendar.getTimeInMillis());
return date1;
}
//獲取當前日期季度
public static Integer getCurrentQuarter() {
Calendar c = Calendar.getInstance();
int currentMonth = c.get(Calendar.MONTH) + 1;
Integer now = 0;
if (currentMonth >= 1 && currentMonth <= 3) {
now = 1;
} else if (currentMonth >= 4 && currentMonth <= 6) {
now = 2;
} else if (currentMonth >= 7 && currentMonth <= 9) {
now = 3;
} else if (currentMonth >= 10 && currentMonth <= 12) {
now = 4;
}
return now;
}
//數字日期和月份的轉化
public static String getMonth(Integer month) {
String mon = null;
if (month == 1) {
mon = "一月份";
} else if (month == 2) {
mon = "二月份";
} else if (month == 3) {
mon = "三月份";
} else if (month == 4) {
mon = "四月份";
} else if (month == 5) {
mon = "五月份";
} else if (month == 6) {
mon = "六月份";
} else if (month == 7) {
mon = "七月份";
} else if (month == 8) {
mon = "八月份";
} else if (month == 9) {
mon = "九月份";
} else if (month == 10) {
mon = "十月份";
} else if (month == 11) {
mon = "十一月份";
} else if (month == 12) {
mon = "十二月份";
}
return mon;
}
//計算本週的週一到周七的所有日期時間
public static List<String> getWeekdays() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
while (calendar.get(Calendar.DAY_OF_WEEK) != FIRST_DAY) {
calendar.add(Calendar.DATE, -1);
}
List<String> list = new ArrayList<String>();
// setToFirstDay(calendar);
for (int i = 0; i < 7; i++) {
// System.out.println(df.format(calendar.getTime()));
list.add(df.format(calendar.getTime()));
calendar.add(Calendar.DATE, 1);
}
return list;
}
//傳進去的d的日期的後6天的日期
public static String getlast(String d) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dt = (Date) sdf.parse(d, new ParsePosition(0));
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(dt);
rightNow.add(Calendar.DATE, 6);// 你要加减的日期
Date dt1 = (Date) rightNow.getTime();
String da = sdf.format(dt1);
return da;
}
// 计算2个日期之间相差的天数
public static int daysBetween(Date smdate, Date bdate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
smdate = sdf.parse(sdf.format(smdate));
bdate = sdf.parse(sdf.format(bdate));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
cal.setTime(smdate);
long time1 = cal.getTimeInMillis();
cal.setTime(bdate);
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(between_days));
}
}
相关文章推荐
- 图片预加载问题
- win8 &win10系统下Guest 帐号本地主机登录
- Windows Server 2008 R2 实现通过WEB方式修改域账号密码
- Glide 入门到精通之四——图片缩放和调整大小
- webservice的使用
- 用URL来操作Hadoop
- 怎么将多张图片合成一个PDF文件
- idea+maven+jetty+jfinal环境搭建
- skcatSgnisueueuQtnemelpmI.232
- 关于set想说的(二)之Set Demo
- 解决Fiddler "creation of the root certificate was not successful”的问题
- 跟着Google学Android —— 1.4 跳转吧!界面!
- 专治Xcode升级后插件失效的问题
- delphi 操作 TWebBrowser 实现自动填表(JQuery脚本与 OleVariant 方法)
- 2016网易笔试-查询互为好友
- windows下配置非安装版的MySQL5.6
- 【编辑器】【Sublime Text】使用笔记
- C++保留的关键字
- Problem G
- WKWebView替换UIWebView