public String getMonthBegin(String strdate) { java.util.Date date = parseFORMatDate(strdate); retur
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@]--结束END--
本文标题: java 日期操作的常用方法
本文链接: https://lsjlt.com/news/233701.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0