文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

java 日期操作的常用方法

2023-06-03 08:14

关注

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() 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");
}


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

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯