package com.cmp.yiwu.util;
import java.util.Calendar;
import java.sql.Date;
import java.text.*;
import java.sql.Time;
import java.sql.Timestamp;
/**
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: Norteksoft</p>
* @author
* @version 1.0
*/
public class DateUtil {
public DateUtil() {
}
public static String getCurrentDate() {
String currentDate = null;
String yyyy = Long.toString(Calendar.getInstance().get(Calendar.YEAR));
String mm = Long.toString(Calendar.getInstance().get(Calendar.MONTH) + 1);
if (mm.length() == 1) {
mm = "0" + mm;
}
String dd = Long.toString(Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
if (dd.length() == 1) {
dd = "0" + dd;
}
currentDate = yyyy + "-" + mm + "-" + dd;
return currentDate;
}
public static Date getNowDate()
{
return Date.valueOf(getCurrentDate());
}
public static String getCurrentTime() {
int h = Calendar.getInstance().get(Calendar.HOUR);
String hour = Long.toString(h);
if (h > 12) {
hour = "上午";
h = h - 12;
}
else if (h < 12) {
hour = "下午";
}
else {
hour = "中午";
}
hour += Long.toString(h) + "点";
int m = Calendar.getInstance().get(Calendar.MINUTE);
String minute = Long.toString(m) + "分";
int s = Calendar.getInstance().get(Calendar.SECOND);
String second = Long.toString(s) + "秒";
String currentTime = hour + ":" + minute + ":" + second;
return currentTime;
}
public static String getCurrentDateAndTime() {
String date = getCurrentDate();
String time = getCurrentTime();
return date + " " + time;
}
private static String[][] timeZoneList = null;
private static Object timeZoneLock = new Object();
public static final long SECOND = 1000;
public static final long MINUTE = 60 * SECOND;
public static final long HOUR = 60 * MINUTE;
public static final long DAY = 24 * HOUR;
public static final long WEEK = 7 * DAY;
//由java.util.Date到java.sql.Date的类型转换
public static java.sql.Date getSqlDate(java.util.Date date) {
return new java.sql.Date(date.getTime());
}
//返回一个日期字符串在星期中的顺序
public static int getDateInWeek(String strDate) {
DateFormat df = DateFormat.getDateInstance();
try {
df.parse(strDate);
java.util.Calendar c = df.getCalendar();
int day = c.get(c.DAY_OF_WEEK) - c.SUNDAY;
return day;
}
catch (ParseException e) {
return -1;
}
}
//一个日期上加天数
public static String DateAdd(String startDate, int dd) {
java.text.DateFormat df = java.text.DateFormat.getDateInstance();
java.util.Date date = new java.util.Date();
try {
date = df.parse(startDate);
}
catch (Exception ex) {
System.out.print(ex);
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, dd);
String mm = String.valueOf(calendar.get(Calendar.MONTH) + 1);
if (mm.length() == 1) {
mm = "0" + mm;
}
String day = String.valueOf(calendar.get(Calendar.DATE));
if (day.length() == 1) {
day = "0" + day;
}
String returnDate = String.valueOf(calendar.get(Calendar.YEAR)) + "-" + mm
+ "-" + day;
return returnDate;
}
//一个日期上加dd个工作日
public static String DateAddWorkday(String startDate, int dd) {
java.text.DateFormat df = java.text.DateFormat.getDateInstance();
java.util.Date date = new java.util.Date();
try {
date = df.parse(startDate);
}
catch (Exception ex) {
System.out.print(ex);
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
while (dd > 0) {
//Calendar.DAY_OF_WEEK返回的 1 是星期天
if ( (calendar.get(Calendar.DAY_OF_WEEK) == 1) ||
(calendar.get(Calendar.DAY_OF_WEEK) == 7)) {
calendar.add(Calendar.DATE, 1);
}
else {
calendar.add(Calendar.DATE, 1);
dd = -1;
}
}
String mm = String.valueOf(calendar.get(Calendar.MONTH) + 1);
String day = String.valueOf(calendar.get(Calendar.DATE));
if (mm.length() == 1) {
mm = "0" + mm;
}
if (day.length() == 1) {
day = "0" + day;
}
String returnDate = String.valueOf(calendar.get(Calendar.YEAR)) + "-" +
mm + "-" + day;
return returnDate;
}
//返回当前日期,类型为格式"yyyy-mm-dd"的字符串
public static String getDate() {
java.text.SimpleDateFormat d = new java.text.SimpleDateFormat();
d.applyPattern("yyyy-MM-dd");
java.util.Date nowdate = new java.util.Date();
String str_date = d.format(nowdate);
return str_date;
}
public static String DateToStr(java.util.Date date) {
java.text.SimpleDateFormat d = new java.text.SimpleDateFormat();
d.applyPattern("yyyy-MM-dd");
String str_date = d.format(date);
return str_date;
}
//返回指定年月的天数
public static int getMonthDayNum(int year, int month) {
if (month == 2) {
return year % 400 != 0 && (year % 4 != 0 || year % 100 == 0) ? 28 : 29;
}
String SmallMonth = ",4,6,9,11,";
return SmallMonth.indexOf(String.valueOf(String.valueOf( (new StringBuffer(
",")).append(String.valueOf(month)).append(",")))) < 0 ? 31 : 30;
}
//返回当前时间
public static String getTime() {
java.util.Date date = new java.util.Date();
java.sql.Time time;
time = new Time(date.getTime());
String strTime = time.toString();
return strTime;
}
public static int getYearMonthDate(String strDate, String style) {
int year;
int month;
int day;
int firstDash;
int secondDash;
if (strDate == null) {
return 0;
}
firstDash = strDate.indexOf('-');
secondDash = strDate.indexOf('-', firstDash + 1);
if ( (firstDash > 0) & (secondDash > 0) &
(secondDash < strDate.length() - 1)) {
year = Integer.parseInt(strDate.substring(0, firstDash));
month = Integer.parseInt(strDate.substring(firstDash + 1, secondDash));
day = Integer.parseInt(strDate.substring(secondDash + 1));
}
else {
return 0;
}
if (style.equalsIgnoreCase("Y")) {
return year;
}
else if (style.equalsIgnoreCase("M")) {
return month;
}
else if (style.equalsIgnoreCase("D")) {
return day;
}
else {
return 0;
}
}
public static int getCurrentHour() {
SimpleDateFormat f = new SimpleDateFormat("HH");
String hh = f.format(new Timestamp(System.currentTimeMillis()));
return Integer.parseInt(hh);
}
//返回两日期差的天数
public static int DateDiff(java.sql.Date date1, java.sql.Date date2) {
int i = (int) ( (date1.getTime() - date2.getTime()) / 3600 / 24 / 1000);
return i;
}
public static java.sql.Timestamp getTimestamp() {
try {
java.text.SimpleDateFormat myFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
java.util.Calendar calendar = java.util.Calendar.getInstance();
String mystrdate = myFormat.format(calendar.getTime());
return java.sql.Timestamp.valueOf(mystrdate);
}
catch (Exception e) {
return null;
}
}
/*****************songwhung***************/
public static String getTimeString() {
try {
java.text.SimpleDateFormat myFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
java.util.Calendar calendar = java.util.Calendar.getInstance();
String mystrdate = myFormat.format(calendar.getTime());
return mystrdate;
}
catch (Exception e) {
return null;
}
}
/*****************songwhung***************/
public static String getCurrentHourMS() {
SimpleDateFormat f = new SimpleDateFormat("HH:MM:SS");
String hh = f.format(new Timestamp(System.currentTimeMillis()));
return hh;
}
public static java.sql.Timestamp getTimestamp(String datestr) {
try {
java.text.SimpleDateFormat myFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String mystrdate = myFormat.format(myFormat.parse(datestr));
return java.sql.Timestamp.valueOf(mystrdate);
}
catch (Exception e) {
return null;
}
}
public static java.sql.Timestamp getDate(String datestr) {
datestr = datestr + " 00:00:00";
try {
java.text.SimpleDateFormat myFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
java.util.Date date = myFormat.parse(datestr);
String mystrdate = myFormat.format(date);
return java.sql.Timestamp.valueOf(mystrdate);
}
catch (Exception e) {
return null;
}
}
public static java.util.Date StrToDate(String strDate) {
int year;
int month;
int day;
int firstDash;
int secondDash;
if (strDate == null) {
return null;
}
firstDash = strDate.indexOf('-');
secondDash = strDate.indexOf('-', firstDash + 1);
if ( (firstDash > 0) & (secondDash > 0) &
(secondDash < strDate.length() - 1)) {
year = Integer.parseInt(strDate.substring(0, firstDash));
month = Integer.parseInt(strDate.substring(firstDash + 1, secondDash));
day = Integer.parseInt(strDate.substring(secondDash + 1));
Calendar c = java.util.Calendar.getInstance();
c.set(year, month - 1, day);
return c.getTime();
}
return null;
}
public static String getDate(java.sql.Date datestr) {
try {
java.text.SimpleDateFormat myFormat = new SimpleDateFormat(
"MM/dd/yyyy");
String mystrdate = myFormat.format(datestr);
return mystrdate;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
/********四舍五入************/
public static float mathRound(float num, int n) {
float realValue = 0;
int dd = 1;
double tempnum = 0;
for (int i = 0; i < n; i++) {
dd *= 10;
}
tempnum = num * dd;
tempnum = Math.round(tempnum);
realValue = (float) tempnum / dd;
return realValue;
}
public static String getTimeStamp() {
SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmmss");
return format.format(new Timestamp(System.currentTimeMillis()));
}
/**
*
* @param style
* @return
*/
public static String getCurrentTime(String style) {
SimpleDateFormat format = new SimpleDateFormat(style);
return format.format(new Timestamp(System.currentTimeMillis()));
}
public static void main(String[] argv) {
DateUtil date = new DateUtil();
System.out.println( (new java.util.Date()));
System.out.println(date.getSqlDate(new java.util.Date()));
System.out.println(date.getDate());
System.out.println(date.getTime());
System.out.println(date.DateAdd("2004-10-10", 2));
System.out.println(date.getTimestamp().toString());
String date2 = date.getTimestamp().toString();
System.out.println(date.StrToDate("2006-02-07"));
}
}
分享到:
相关推荐
Java中的日期处理涉及到多个类,包括Date、Calendar、GregorianCalendar以及日期格式化类如DateFormat和SimpleDateFormat。这些类主要用于管理、操作和格式化日期和时间。以下是对这些类的详细说明: 1. **Date类**...
C++标准库并没有内置一个强大的日期处理类,但可以通过自定义类或者使用第三方库来实现这一功能。本篇文章将深入探讨如何在C++中创建一个强大的日期处理类,涵盖日期的加减、判断星期几以及识别闰年等核心知识点。 ...
### Java中的日期处理类总结:Date、Calendar、GregorianCalendar、DateFormat、SimpleDateFormat 在Java编程中,日期时间处理是常见的需求之一。为了更好地理解和使用Java中的日期处理类,本篇文章将对Date、...
DateHandler.java日期处理类Java文件下载,有些功能很不错哦,比如计算是否是季度末、计算从现在开始几天后的时间、得到当前时间,用于文件名,使用yyyyMMddHHmmss格式、比较日期,与现在-N天的日期对比、比较日期,...
工作分享:java中的日期处理类总结
自己整理的Java日期处理类 。包含获取两个之间段之间的天数、月初日期,月末日期、一个时间段之间的所有的日期,月份 一周的第一天,最后一天、 当前是周几等。仅供参考
Java中的日期处理是软件开发中经常遇到的问题。在Java中,处理日期的类主要分为以下几个部分:java.util.Date类、java.util.Calendar类、java.util.GregorianCalendar类、java.text.DateFormat类和java.text....
针对时间日期各种格式之间转换等操作的公用类。
Java实现的日期处理类完整实例 Java实现的日期处理类是Java编程语言中对日期的处理和操作的集合,包括日期的获取、运算、转换等相关操作技巧。本文将详细介绍Java实现的日期处理类,结合完整实例形式分析了Java针对...
"日期处理工具类"通常是指一个自定义的类,它封装了一些常用的方法,例如日期格式化、字符串转换为日期以及对日期进行加减操作。这个类可能是开发者为了提高代码复用性和简化日期处理逻辑而创建的。 首先,让我们...
本文将介绍如何实现一个功能强大的时间日期处理类。 首先,我们讨论的类称为DateTimeHelper,它通过封装常用的时间日期判断与计算功能,为用户提供了便利。在C#中,处理日期和时间主要涉及到System.DateTime类。该...
在Java中,日期时间处理类的情况有所不同。Java 8之前,主要使用`java.util.Date`和`java.text.SimpleDateFormat`进行日期时间的处理,但这两个类的设计被认为不够理想。自Java 8起,引入了`java.time`包,其中包括`...
用于所有的与日期相关的格式转化的工具类 * 将date转换为日历 public static Calendar date2Calendar(Date date) { Calendar calendar = Calendar.getInstance(); // 得到日历 calendar.setTime(date); ...