java 日期操作的常用方法
public String getMonthBegin(String strdate)
{
java.util.Date date = parseFormatDate(strdate);
return formatDateByFormat(date,"yyyy-MM") + "-01";
}
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());
}
public String formatDate(java.util.Date date)
{
return formatDateByFormat(date,"yyyy-MM-dd");
}
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;
}
public class DateUtil {
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()
}
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");
}
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");
}
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);
}
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;
}
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);
}
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);
}
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);
}
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);
}
public static long getMillis(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.getTimeInMillis();
}
public static String getDate(java.util.Date date) {
return format(date, "yyyy/MM/dd");
}
public static String getTime(java.util.Date date) {
return format(date, "HH:mm:ss");
}
public static String getDateTime(java.util.Date date) {
return format(date, "yyyy/MM/dd HH:mm:ss");
}
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();
}
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){
}
}
[@more@]免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341