首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

java取得当前年、月、季度、周、日、时日期时间代码

2012-09-14 
java获得当前年、月、季度、周、日、时日期时间代码package com.util.dateimport java.text.SimpleDateFormat

java获得当前年、月、季度、周、日、时日期时间代码

package com.util.date;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
?*?User:?冯玉敏
?* Date: 11-8-9
?* Time: 下午1:23
?*/
public class DateUtil {
??? private static SimpleDateFormat bigLongSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSSS");
??? private static SimpleDateFormat hourSdf = new SimpleDateFormat("HH");
??? private static SimpleDateFormat minutesSdf = new SimpleDateFormat("mm");
??? private static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
??? private static SimpleDateFormat longHourSdf = new SimpleDateFormat("yyyy-MM-dd HH");
??? private static SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


??? /**
???? * 获得本周的第一天,即周日
???? *
???? * @return
???? */
??? public static Date getCurrentWeekDayStartTime() {
??????? Calendar c = Calendar.getInstance();
??????? try {
??????????? int weekday = c.get(Calendar.DAY_OF_WEEK) - 1;
??????????? c.add(Calendar.DATE, -weekday);
??????????? c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"));
??????? } catch (Exception e) {
??????????? e.printStackTrace();
??????? }
??????? return c.getTime();
??? }

??? /**
???? * 获得本周的最后一天,即本周六
???? *
???? * @return
???? */
??? public static Date getCurrentWeekDayEndTime() {
??????? Calendar c = Calendar.getInstance();
??????? try {
??????????? int weekday = c.get(Calendar.DAY_OF_WEEK);
??????????? c.add(Calendar.DATE, 7 - weekday);
??????????? c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"));
??????? } catch (Exception e) {
??????????? e.printStackTrace();
??????? }
??????? return c.getTime();
??? }

??? /**
???? * 获得本天的开始时间,即2012-01-01 00:00:00
???? *
???? * @return
???? */
??? public static Date getCurrentDayStartTime() {
??????? Date now = new Date();
??????? try {
??????????? now = shortSdf.parse(shortSdf.format(now));
??????? } catch (Exception e) {
??????????? e.printStackTrace();
??????? }
??????? return now;
??? }

??? /**
???? * 获得本天的结束时间,即2012-01-01 23:59:59
???? *
???? * @return
???? */
??? public static Date getCurrentDayEndTime() {
??????? Date now = new Date();
??????? try {
??????????? now = longSdf.parse(shortSdf.format(now) + " 23:59:59");
??????? } catch (Exception e) {
??????????? e.printStackTrace();
??????? }
??????? return now;
??? }

??? /**
???? * 获得本小时的开始时间,即2012-01-01 23:59:59
???? *
???? * @return
???? */
??? public static Date getCurrentHourStartTime() {
??????? Date now = new Date();
??????? try {
??????????? now = longHourSdf.parse(longHourSdf.format(now));
??????? } catch (Exception e) {
??????????? e.printStackTrace();
??????? }
??????? return now;
??? }

??? /**
???? * 获得本小时的结束时间,即2012-01-01 23:59:59
???? *
???? * @return
???? */
??? public static Date getCurrentHourEndTime() {
??????? Date now = new Date();
??????? try {
??????????? now = longSdf.parse(longHourSdf.format(now) + ":59:59");
??????? } catch (Exception e) {
??????????? e.printStackTrace();
??????? }
??????? return now;
??? }

??? /**
???? * 获得本月的开始时间,即2012-01-01 00:00:00
???? *
???? * @return
???? */
??? public static Date getCurrentMonthStartTime() {
??????? Calendar c = Calendar.getInstance();
??????? Date now = null;
??????? try {
??????????? c.set(Calendar.DATE, 1);
??????????? now = shortSdf.parse(shortSdf.format(c.getTime()));
??????? } catch (Exception e) {
??????????? e.printStackTrace();
??????? }
??????? return now;
??? }

??? /**
???? * 当前月的结束时间,即2012-01-31 23:59:59
???? *
???? * @return
???? */
??? public static Date getCurrentMonthEndTime() {
??????? Calendar c = Calendar.getInstance();
??????? Date now = null;
??????? try {
??????????? c.set(Calendar.DATE, 1);
??????????? c.add(Calendar.MONTH, 1);
??????????? c.add(Calendar.DATE, -1);
??????????? now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
??????? } catch (Exception e) {
??????????? e.printStackTrace();
??????? }
??????? return now;
??? }


??? /**
???? * 当前年的开始时间,即2012-01-01 00:00:00
???? *
???? * @return
???? */
??? public static Date getCurrentYearStartTime() {
??????? Calendar c = Calendar.getInstance();
??????? Date now = null;
??????? try {
??????????? c.set(Calendar.MONTH, 0);
??????????? c.set(Calendar.DATE, 1);
??????????? now = shortSdf.parse(shortSdf.format(c.getTime()));
??????? } catch (Exception e) {
??????????? e.printStackTrace();
??????? }
??????? return now;
??? }

??? /**
???? * 当前年的结束时间,即2012-12-31 23:59:59
???? *
???? * @return
???? */
??? public static Date getCurrentYearEndTime() {
??????? Calendar c = Calendar.getInstance();
??????? Date now = null;
??????? try {
??????????? c.set(Calendar.MONTH, 11);
??????????? c.set(Calendar.DATE, 31);
??????????? now = shortSdf.parse(shortSdf.format(c.getTime()));
??????? } catch (Exception e) {
??????????? e.printStackTrace();
??????? }
??????? return now;
??? }

??? /**
???? * 当前季度的开始时间,即2012-01-1 00:00:00
???? *
???? * @return
???? */
??? public static Date getCurrentQuarterStartTime() {
??????? Calendar c = Calendar.getInstance();
??????? int currentMonth = c.get(Calendar.MONTH) + 1;
??????? Date 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 = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
??????? } catch (Exception e) {
??????????? e.printStackTrace();
??????? }
??????? return now;
??? }

??? /**
???? * 当前季度的结束时间,即2012-03-31 23:59:59
???? *
???? * @return
???? */
??? public static Date getCurrentQuarterEndTime() {
??????? Calendar c = Calendar.getInstance();
??????? int currentMonth = c.get(Calendar.MONTH) + 1;
??????? Date 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 = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
??????? } catch (Exception e) {
??????????? e.printStackTrace();
??????? }

??????? return now;
??? }

??? public static void main(String args[]) {
??????? System.out.println("当前年开始时间:" + longSdf.format(getCurrentYearStartTime()));
??????? System.out.println("当前年结束时间:" + longSdf.format(getCurrentYearEndTime()));
??????? System.out.println("-------------------");
??????? System.out.println("当前季度开始时间:" + longSdf.format(getCurrentQuarterStartTime()));
??????? System.out.println("当前季度结束时间:" + longSdf.format(getCurrentQuarterEndTime()));
??????? System.out.println("-------------------");
??????? System.out.println("当前月开始时间:" + longSdf.format(getCurrentMonthStartTime()));
??????? System.out.println("当前月结束时间:" + longSdf.format(getCurrentMonthEndTime()));
??????? System.out.println("-------------------");
??????? System.out.println("当前周开始时间:" + longSdf.format(getCurrentWeekDayStartTime()));
??????? System.out.println("当前周结束时间:" + longSdf.format(getCurrentWeekDayEndTime()));
??????? System.out.println("-------------------");
??????? System.out.println("当前天开始时间:" + longSdf.format(getCurrentDayStartTime()));
??????? System.out.println("当前天结束时间:" + longSdf.format(getCurrentDayEndTime()));
??????? System.out.println("-------------------");
??????? System.out.println("当前时开始时间:" + longSdf.format(getCurrentHourStartTime()));
??????? System.out.println("当前时结束时间:" + longSdf.format(getCurrentHourEndTime()));

??? }

}

热点排行