码迷,mamicode.com
首页 > 编程语言 > 详细

Java date相关 格式化(二)

时间:2017-11-14 18:34:43      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:ast   java   try   gety   tla   form   获取   时间   oda   

import java.util.Calendar;

import java.util.Date;
import java.util.GregorianCalendar;

public class ZCalendarUtils {

/**
* 取去年的今天
* @return
*/
public static String getLastYear() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, -1);
Date d = cal.getTime();
return DateTimeUtils.formatToDateTime(d);
}

/**
* 获取某一天是星期几
* @param dt
* @return
*/
public static String getWeekOfDate(Date dt) {
String[] weekDays = {"7", "1", "2", "3", "4", "5", "6"};
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0){
w = 0;
}
return weekDays[w];
}
/**
* 取当前是星期几
* @return
*/
public static String getCurrentWeek() {
String day = getWeekOfDate(new Date());
return day;
}

/**
* 取当前月第一天
* @return
*/
public static String getFirstDayOfCurrentMonth() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);
Date d = cal.getTime();
return DateTimeUtils.formatToDate(d);
}

/**
* 取昨天
* @return
*/
public static String getYesterday() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -1);
Date d = cal.getTime();
return DateTimeUtils.formatToDate(d);
}

/**
* 取当前月最后一天
* @return
*/
public static String getLastDayOfCurrentMonth() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, 1);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.add(Calendar.DAY_OF_MONTH, -1);
Date d = cal.getTime();
return DateTimeUtils.formatToDate(d);
}
/**
* 取上月第一天
* @return
*/
public static String getFirstDayOfLastMonth() {
Calendar cal = Calendar.getInstance();
cal.add(cal.MONTH, -1);
cal.set(Calendar.DAY_OF_MONTH, 1);
Date d = cal.getTime();
return DateTimeUtils.formatToDate(d);
}
/**
* 取上月最后一天
* @return
*/
public static String getLastDayOfLastMonth() {
Calendar cal = Calendar.getInstance();
//取得系统当前时间所在月第一天时间对象
cal.set(Calendar.DAY_OF_MONTH, 1);
//日期减一,取得上月最后一天时间对象
cal.add(Calendar.DAY_OF_MONTH, -1);
Date d = cal.getTime();
return DateTimeUtils.formatToDate(d);
}

/**
* 取当前是本月第几周
* @return
*/
public static int getWeekNumber(){
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(new Date());
return calendar.get(Calendar.WEEK_OF_MONTH) - 1;
}
/**
* 取给定日期所在月份的第几周
* @param newDate
* @param week
* @return
*/
public static Calendar getCalendar(Date newDate, int week) {
Calendar caleNew = Calendar.getInstance();
caleNew.setTime(newDate);
caleNew.add(Calendar.WEEK_OF_MONTH, week -1 );
return caleNew;
}
/**
* 获上星期的第一天
*
* @return
*/
public static String getFirstDayOfLastWeek() {
int currentWeekNo = ZCalendarUtils.getWeekNumber();
Date date = ZCalendarUtils.getFirstDayOfWeek(new Date(), currentWeekNo -1);
return DateTimeUtils.formatToDate(date);
}
/**
* 获取当前星期的第一天
*
* @return
*/
public static String getFirstDayOfCurrentWeek() {
int currentWeekNo = ZCalendarUtils.getWeekNumber();
Date date = ZCalendarUtils.getFirstDayOfWeek(new Date(), currentWeekNo);
return DateTimeUtils.formatToDate(date);
}
/**
* 获取上星期的最后一天
*
* @return
*/
public static String getLastDayOfLastWeek() {
int currentWeekNo = ZCalendarUtils.getWeekNumber();
Date date = ZCalendarUtils.getLastDayOfWeek(new Date(), currentWeekNo -1);
return DateTimeUtils.formatToDate(date);
}
/**
* 获取当前星期的最后一天
*
* @return
*/
public static String getLastDayOfCurrentWeek() {
int currentWeekNo = ZCalendarUtils.getWeekNumber();
Date date = ZCalendarUtils.getLastDayOfWeek(new Date(), currentWeekNo);
return DateTimeUtils.formatToDate(date);
}

/**
* 获取给定日期所在月的第n星期的第一天
*
* @return
*/
private static Date getFirstDayOfWeek(Date month, int week) {
Calendar myCale = Calendar.getInstance();
try{
GregorianCalendar gc = (GregorianCalendar)getCalendar(month, week );
myCale.setTime(gc.getTime());
myCale.set(Calendar.DATE, gc.get(Calendar.DATE) - gc.get(Calendar.DAY_OF_WEEK) + 2);
}catch (Exception e) {
e.printStackTrace();
}
return myCale.getTime();
}

/**
* 获取给定日期所在月的第n星期的最后一天
*
* @return
*/
private static Date getLastDayOfWeek(Date month, int week) {
Calendar myCale = Calendar.getInstance();
try{
GregorianCalendar gc = (GregorianCalendar)getCalendar(month, week);
myCale.setTime(gc.getTime());
myCale.set(Calendar.DATE, gc.get(Calendar.DATE) + 8 - gc.get(Calendar.DAY_OF_WEEK));
}catch (Exception e) {
e.printStackTrace();
}
return myCale.getTime();
}


public static int getYearOfDate(Date date) {
if (date == null) {
return 1990;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.YEAR);
}

public static int getMonthOfDate(Date date) {
if (date == null) {
return 1;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MONTH) + 1;
}

public static int getDayOfDate(Date date) {
if (date == null) {
return 1;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_MONTH);
}

public static int getCurrentMonth() {
return getMonthOfDate(new Date());
}

public static String getCurrentMonthStr() {
return getCurrentMonth() + "";
}

public static String getMonthOfDateStr(Date date) {
return getMonthOfDate(date) + "";
}

/**
* 获取某日期所在月份的最大天数
*
* @param date
* @return
*/
public static int getMaxDaysOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}

}

Java date相关 格式化(二)

标签:ast   java   try   gety   tla   form   获取   时间   oda   

原文地址:http://www.cnblogs.com/Yorkzhu/p/7833721.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!