/**
* 取得指定月份的第一天
*
* @param strdate String
* @return String
*/
public String getMonthBegin(String strdate)
{
java.util.Date date = parseFormatDate(strdate);
return formatDateByFormat(date,"yyyy-MM") + "-01";
}
/**
* 取得指定月份的最后一天
*
* @param strdate String
* @return String
*/
public String getMonthEnd(String strdate)
{
java.util.Date date = parseFormatDate(getMonthBegin(strdate));
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH,1);
calendar.add(Calendar.DAY_OF_YEAR, -1);
return formatDate(calendar.getTime());
}
/**
* 常用的格式化日期
*
* @param date Date
* @return String
*/
public String formatDate(java.util.Date date)
{
return formatDateByFormat(date,"yyyy-MM-dd");
}
/**
* 以指定的格式来格式化日期
*
* @param date Date
* @param format String
* @return String
*/
public String formatDateByFormat(java.util.Date date,String format)
{
String result = "";
if(date != null)
{
try
{
SimpleDateFormat sdf = new SimpleDateFormat(format);
result = sdf.format(date);
}
catch(Exception ex)
{
LOGGER.info("date:" + date);
ex.printStackTrace();
}
}
return result;
}
/**
* 日期操作
*
* @author xxx
* @version 2.0 jdk1.4.0 tomcat5.1.0 * Updated Date:2005/03/10
*/
public class DateUtil {
/**
* 格式化日期
*
* @param dateStr
* 字符型日期
* @param format
* 格式
* @return 返回日期
*/
public static java.util.Date parseDate(String dateStr, String format) {
java.util.Date date = null;
try {
java.text.DateFormat df = new java.text.SimpleDateFormat(format);
String dt=Normal.parse(dateStr).replaceAll(
"-", "/");
if((!dt.equals(""))&&(dt.length()<format.length())){
dt+=format.substring(dt.length()).replaceAll("[YyMmDdHhSs]","0");
}
date = (java.util.Date) df.parse(dt);
} catch (Exception e) {
}
return date;
}
public static java.util.Date parseDate(String dateStr) {
return parseDate(dateStr, "yyyy/MM/dd");
}
public static java.util.Date parseDate(java.sql.Date date) {
return date;
}
public static java.sql.Date parseSqlDate(java.util.Date date) {
if (date != null)
return new java.sql.Date(date.getTime());
else
return null;
}
public static java.sql.Date parseSqlDate(String dateStr, String format) {
java.util.Date date = parseDate(dateStr, format);
return parseSqlDate(date);
}
public static java.sql.Date parseSqlDate(String dateStr) {
return parseSqlDate(dateStr, "yyyy/MM/dd");
}
public static java.sql.Timestamp parseTimestamp(String dateStr,
String format) {
java.util.Date date = parseDate(dateStr, format);
if (date != null) {
long t = date.getTime();
return new java.sql.Timestamp(t);
} else
return null;
}
public static java.sql.Timestamp parseTimestamp(String dateStr) {
return parseTimestamp(dateStr, "yyyy/MM/dd HH:mm:ss");
}
/**
* 格式化输出日期
*
* @param date
* 日期
* @param format
* 格式
* @return 返回字符型日期
*/
public static String format(java.util.Date date, String format) {
String result = "";
try {
if (date != null) {
java.text.DateFormat df = new java.text.SimpleDateFormat(format);
result = df.format(date);
}
} catch (Exception e) {
}
return result;
}
public static String format(java.util.Date date) {
return format(date, "yyyy/MM/dd");
}
/**
* 返回年份
*
* @param date
* 日期
* @return 返回年份
*/
public static int getYear(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.YEAR);
}
/**
* 返回月份
*
* @param date
* 日期
* @return 返回月份
*/
public static int getMonth(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.MONTH) + 1;
}
/**
* 返回日份
*
* @param date
* 日期
* @return 返回日份
*/
public static int getDay(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.DAY_OF_MONTH);
}
/**
* 返回小时
*
* @param date
* 日期
* @return 返回小时
*/
public static int getHour(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.HOUR_OF_DAY);
}
/**
* 返回分钟
*
* @param date
* 日期
* @return 返回分钟
*/
public static int getMinute(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.MINUTE);
}
/**
* 返回秒钟
*
* @param date
* 日期
* @return 返回秒钟
*/
public static int getSecond(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.SECOND);
}
/**
* 返回毫秒
*
* @param date
* 日期
* @return 返回毫秒
*/
public static long getMillis(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.getTimeInMillis();
}
/**
* 返回字符型日期
*
* @param date
* 日期
* @return 返回字符型日期
*/
public static String getDate(java.util.Date date) {
return format(date, "yyyy/MM/dd");
}
/**
* 返回字符型时间
*
* @param date
* 日期
* @return 返回字符型时间
*/
public static String getTime(java.util.Date date) {
return format(date, "HH:mm:ss");
}
/**
* 返回字符型日期时间
*
* @param date
* 日期
* @return 返回字符型日期时间
*/
public static String getDateTime(java.util.Date date) {
return format(date, "yyyy/MM/dd HH:mm:ss");
}
/**
* 日期相加
*
* @param date
* 日期
* @param day
* 天数
* @return 返回相加后的日期
*/
public static java.util.Date addDate(java.util.Date date, int day) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000);
return c.getTime();
}
/**
* 日期相减
*
* @param date
* 日期
* @param date1
* 日期
* @return 返回相减后的日期
*/
public static int diffDate(java.util.Date date, java.util.Date date1) {
return (int) ((getMillis(date) - getMillis(date1)) / (24 * 3600 * 1000));
}
}
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
int date = now.get(Calendar.DAY_OF_MONTH);
int month = now.get(Calendar.MONTH) + 1;
int hour = now.get(Calendar.HOUR);
int min = now.get(Calendar.MINUTE);
int sec = now.get(Calendar.SECOND);
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
/**
*
*
public class ManageWeek {
//判断两个日期是否在同一周
boolean isSameWeekDates(Date date1, Date date2) {
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(date1);
cal2.setTime(date2);
int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
if (0 == subYear) {
if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
return true;
}
else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
// 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
return true;
}
else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
return true;
}
return false;
}
//产生周序列
public static String getSeqWeek(){
Calendar c = Calendar.getInstance(Locale.CHINA);
String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
if(week.length()==1)week = "0" + week;
String year = Integer.toString(c.get(Calendar.YEAR));
return year+week;
}
//获得周一的日期
public static String getMonday(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
}
//获得周五的日期
public static String getFriday(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK,Calendar.FRIDAY);
return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
}
public static void main(String[] args){
}
}
java日期操作常用方法
点赞
收藏