`

java date和hibernate date

阅读更多
此文转载自:leonelwong的博客,个人觉得很好,就收藏了。这里谢谢leonelwong

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
* @author Leonel Wong
* @version 1.0.0
* @create 2008-07-24 10:10
* @see 处理时间(包括时间格式的类)
*/
public class DateUtils {
/**
* 取时间年的格式代码
*/
public static String YEAR = "yyyy";

/**
* 取时间月的格式代码
*/
public static String MONTH = "MM";

/**
* 取时间日的格式代码
*/
public static String DAY = "dd";

/**
* 取时间时的格式代码
*/
public static String HOUR = "hh";

/**
* 取时间24小时制的格式代码
*/
public static String HOUR_24 = "HH";

/**
* 取时间分的格式代码
*/
public static String MIMUTE = "mm";

/**
* 取时间秒的格式代码
*/
public static String SECOND = "ss";

/**
* 取时间毫秒的格式代码
*/
public static String MILLISECOND = "SS";

/**
* 格式为yyyy-MM-dd的时间
*/
public static String YMD_FORMAT = YEAR + "-" + MONTH + "-" + DAY;
/**
* 格式为yyyy-MM-dd HH:mm:ss的时间
*/
public static String YMDHMS_FORMAT = YEAR + "-" + MONTH + "-" + DAY + " "
+ HOUR_24 + ":" + MIMUTE + ":" + SECOND;

/**
* 格式为yyyy-MM-dd HH:mm:ss:SS的时间
*/
public static String UTILTIME_FORMAT = YEAR + "-" + MONTH + "-" + DAY + " "
+ HOUR_24 + ":" + MIMUTE + ":" + SECOND + ":" + MILLISECOND;

/**
* 格式为yyyyMMddHHmmssSS的时间
*/
public static String CRITERIONTIME_FORMAT = YEAR + MONTH + DAY + HOUR_24
+ MIMUTE + SECOND + MILLISECOND;

/**
* @author 王磊
* @see 2008-03-08 获得yy-mm-dd格式的时间
* @return String date
*/
public static String getYearMonthDay() {
return getDateByFormat(YEAR + "-" + MONTH + "-" + DAY);
}

/**
* @see 返回格式为yyyy-MM-dd HH:mm:ss:SS的时间字符串
* @return String date
*/
public static String getNOWTime_0() {
return getDateByFormat(UTILTIME_FORMAT);
}

/**
* @see 返回格式为yyyyMMddHHmmssSS的时间字符串
* @return String date
*/
public static String getNOWTime_1() {
return getDateByFormat(CRITERIONTIME_FORMAT);
}

/**
* @see 获得指定时间格式
* @param String
*            format 时间格式
* @return String dateStr 返回获得相应格式时间的字符串
*/
public static String getDateByFormat(String format) {
String dateStr = "";
try {
if (format != null) {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat simFormat = new SimpleDateFormat(format,
Locale.CHINA);
dateStr = simFormat.format(date);
}
} catch (Exception e) {
e.printStackTrace();
}
return dateStr;
}

/**
* @see 获得指定时间格式
* @param Date
*            date 时间
* @param String
*            format 时间格式
* @return String dateStr 返回获得相应格式时间的字符串
*/
public static String getDateByFormatYMD(Date date) {
String dateStr = "";
try {
SimpleDateFormat simFormat = new SimpleDateFormat(YMD_FORMAT,
Locale.CHINA);
dateStr = simFormat.format(date);
} catch (Exception e) {
e.printStackTrace();
}
return dateStr;
}

/**
* @see 获得指定时间格式
* @param Date
*            date 时间
* @param String
*            format 时间格式
* @return String dateStr 返回获得相应格式时间的字符串
*/
public static String getDateByFormat(Date date, String format) {
String dateStr = "";
try {
if (format != null) {
SimpleDateFormat simFormat = new SimpleDateFormat(format,
Locale.CHINA);
dateStr = simFormat.format(date);
}
} catch (Exception e) {
e.printStackTrace();
}
return dateStr;
}

/**
* @see 获得当前时间
* @return Date date
*/
public static Date getNOWTime() {
return new Date(System.currentTimeMillis());
}

/**
* @see 把字符串类型的时间转换为yyyy-MM-dd的时间格式
*/
public static Date getDateByStrToYMD(String str) {
Date date = null;
if (str != null && str.trim().length() > 0) {
DateFormat dFormat = new SimpleDateFormat(YMD_FORMAT);
try {
date = dFormat.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
}
return date;
}

/**
* @see 把字符串类型的时间转换为自定义格式的时间
*/
public static Date getDateByStrToFormat(String format, String str) {
DateFormat dFormat = new SimpleDateFormat(format);
Date date = null;
try {
if (str != null) {
date = dFormat.parse(str);
}
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}

/**
* 功能:判断输入年份是否为闰年<br>
*
* @param year
* @return 是:true 否:false
* @author pure
*/
public static boolean leapYear(int year) {
boolean leap;
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
leap = true;
} else {
leap = false;
}
} else {
leap = true;
}
} else
leap = false;
return leap;
}

/**
* 功能:得到指定月份的月底 格式为:xxxx-yy-zz (eg: 2007-12-31)<br>
*
* @param String
* @return String
*/
public static String getEndOfMonth(String str) {
int tyear = Integer.parseInt(getDateByFormat(getDateByStrToYMD(str),
YEAR));
int tmonth = Integer.parseInt(getDateByFormat(getDateByStrToYMD(str),
MONTH));
String strtmonth = null;
String strZ = null;
if (tmonth == 1 || tmonth == 3 || tmonth == 5 || tmonth == 7
|| tmonth == 8 || tmonth == 10 || tmonth == 12) {
strZ = "31";
}
if (tmonth == 4 || tmonth == 6 || tmonth == 9 || tmonth == 11) {
strZ = "30";
}
if (tmonth == 2) {
if (leapYear(tyear)) {
strZ = "29";
} else {
strZ = "28";
}
}
strtmonth = tmonth >= 10 ? String.valueOf(tmonth) : ("0" + tmonth);
return tyear + "-" + strtmonth + "-" + strZ;
}

public static String getEndOfMonth(int tyear, int tmonth) {
String strtmonth = null;
String strZ = null;
if (tmonth == 1 || tmonth == 3 || tmonth == 5 || tmonth == 7
|| tmonth == 8 || tmonth == 10 || tmonth == 12) {
strZ = "31";
}
if (tmonth == 4 || tmonth == 6 || tmonth == 9 || tmonth == 11) {
strZ = "30";
}
if (tmonth == 2) {
if (leapYear(tyear)) {
strZ = "29";
} else {
strZ = "28";
}
}
strtmonth = tmonth >= 10 ? String.valueOf(tmonth) : ("0" + tmonth);
return tyear + "-" + strtmonth + "-" + strZ;
}

/**
* 功能:得到指定月份的月初 格式为:xxxx-yy-zz (eg: 2007-12-01)<br>
*
* @param String
* @return String
*/
public static String getStartOfMonth(int tyear, int tmonth) {
String strtmonth = tmonth >= 10 ? String.valueOf(tmonth)
: ("0" + tmonth);
return tyear + "-" + strtmonth + "-" + "01";
}

public static String getStartOfMonth(String str) {
int tyear = Integer.parseInt(getDateByFormat(getDateByStrToYMD(str),
YEAR));
int tmonth = Integer.parseInt(getDateByFormat(getDateByStrToYMD(str),
MONTH));
String strtmonth = tmonth >= 10 ? String.valueOf(tmonth)
: ("0" + tmonth);
return tyear + "-" + strtmonth + "-" + "01";
}

/**
* 功能:得到指定月份的月初 格式为:xxxx-yy-zz (eg: 2007-12-01)<br>
*
* @param String
* @return String
*/
public static int getMonthCountBySQU(String start, String end) {
int syear = Integer.parseInt(getDateByFormat(getDateByStrToYMD(start),
YEAR));
int smonth = Integer.parseInt(getDateByFormat(getDateByStrToYMD(start),
MONTH));
int eyear = Integer.parseInt(getDateByFormat(getDateByStrToYMD(start),
YEAR));
int emonth = Integer.parseInt(getDateByFormat(getDateByStrToYMD(start),
MONTH));
return (eyear - syear) * 12 + (emonth - smonth) + 1;
}

/**
* 获得时间序列 EG:2008-01-01~2008-01-31,2008-02-01~2008-02-29
*/
public static List getMonthSqu(String fromDate, String toDate) {
List list = new ArrayList();
int count = getMonthCountBySQU(fromDate, toDate);
int syear = Integer.parseInt(getDateByFormat(
getDateByStrToYMD(fromDate), YEAR));
int smonth = Integer.parseInt(getDateByFormat(
getDateByStrToYMD(fromDate), MONTH));
int eyear = Integer.parseInt(getDateByFormat(getDateByStrToYMD(toDate),
YEAR));
String startDate = fromDate;
String endDate = "";
for (int i = 1; i <= count; i++) {
if (syear <= eyear) {
startDate = getStartOfMonth(syear, smonth);
endDate = getEndOfMonth(syear, smonth);
list.add(startDate + "~" + endDate);
System.out.println(startDate + "~" + endDate);
if (smonth == 13) {
smonth = 1;
syear++;
}
smonth++;
}
}
return list;
}
/**
* 通过传入的时间来获得所属周内的时间
* @param start
* @param num
* @return
*/
public static String getDateOFWeekByDate(String start, int num) {
Date dd = getDateByStrToYMD(start);
Calendar c = Calendar.getInstance();
c.setTime(dd);
if (num==1) // 返回星期一所在的日期
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
else if (num==2) // 返回星期二所在的日期
c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
else if (num==3) // 返回星期三所在的日期
c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
else if (num==4) // 返回星期四所在的日期
c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
else if (num==5) // 返回星期五所在的日期
c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
else if (num==6) // 返回星期六所在的日期
c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
else if (num==0) // 返回星期日所在的日期
c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
return getDateByFormatYMD(c.getTime());
}
} 
分享到:
评论
1 楼 hekai1990 2012-05-03  
受教了...

相关推荐

    jdbc操作数据库之Date类型

    但是jdbc对应操作date类型的数据和hibernate是不同的,hibernate不需要考虑date是util .date还是sql.date也不需要考虑存入的date是否存有时分秒了。

    Hibernate注释大全收藏

    @TemporalType.(DATE,TIME,TIMESTAMP) 分别Map java.sql.(Date, Time, Timestamp). @Lob 注解属性将被持久化为 Blog 或 Clob 类型。具体的java.sql.Clob, Character[], char[] 和 java.lang.String 将被持久化为 ...

    Hibernate Search In Action

    the Hibernate and Java Persistence development paradigm. This library relieves you of the burdens of keeping indexes up to date with the database, converts Lucene results into managed objects of your ...

    Hibernate中文API

    你可看到Hibernate可以直接访问public,private和protected的访问方法和field。所以选择哪种方式完全取决于你,你可以使你的选择与你的应用程序设计相吻合。 所有的持久化类(persistent classes)都要求有无参的...

    Hibernate中文API大全

    尝试对这个组合元素重新建模为一个实体-但是需要注意的是,虽然Java模型和重新建模前是一样的,关系模型和持久性语义会有细微的变化。 请注意如果你使用标签,一个组合元素的映射不支持可能为空的属性. 当删除...

    Hibernate 中文API文档

    Hibernate不仅仅管理Java类到数据库表的映射(包括Java数据类型到SQL数据类型的映射),还提供数据查询和获取数据的方法,可以大幅度减少开发时人工使用SQL和JDBC处理数据的时间。 Hibernate的目标是对于开发者...

    (超赞)JAVA精华之--深入JAVA API

    3.4 hibernate的hello word 3.5 JavaMail(JAVA邮件服务)API详解 3.6 jxl.jar 包简介 3.7 Java与XML联合编程之SAX篇 3.8 Java与XML联合编程之DOM篇 4 其他 4.1 代码复用的规则 4.2 Java IO 包中的Decorator模式

    hibernate总结

    持久化类的对象,在hibernate应用中可以处于三种状态(根据对象和session之间的关系进行划分): 1. 临时态,瞬态:特点: a) 在数据库中没有记录和它对应 b) 和session没有任何关系 c) New 出来的对象,都处于临时...

    Hibernate Recipes. A Problem Solution Approach (英文)

    This book is for experienced Java developers looking to use Hibernate, but is also appropriate for Java developers new to Hibernate. Pub date: Jun 2010

    mysql+jdbc+jsp+Hibernate3.2+tomcattomcat5.028成功测试

    charSet=GBK" %&gt; &lt;%@ page import="java.util.*" %&gt; &lt;%@ page import="org.hibernate.SessionFactory" %&gt; &lt;%@ page import="org.hibernate.Session" %&gt; &lt;%@ page import="org.hibernate.cfg.*" %&gt; &lt;%...

    Hibernate4注解+Struts2例子

    Hibernate4注解+Struts2的例子,里面详细介绍了怎么正确搭建Hibernate4,怎么使用注解生成实体类,怎么使用Hibernate4的二级缓存Ehcache,完整的增删查改功能,附带扁平化列表的效果图和里面关键配置的学习文档,让...

    JAVA SE学习精华集锦

    3.4 hibernate的hello word 143 3.5 JavaMail(JAVA邮件服务)API详解 145 3.6 jxl.jar 包简介 150 3.7 Java与XML联合编程之SAX篇 154 3.8 Java与XML联合编程之DOM篇 159 4 其他 165 4.1 代码复用的规则 165 4.2 ...

    2017 spring data jpa+spring4.2+springmvc+hibernate4.3 maven环境intellij idea增删改查实例

    2017 spring data jpa+spring4.2+springmvc+hibernate4.3 maven环境intellij idea增删改查实例

    mastering.java

    it has been around since 1991 and, to date, has proved to be the most popular, especially where web development is concerned. It is one of the simplest languages to learn, and it is object-oriented,...

    Myeclipse开发struts+hibernate+spring新手入门--环境配置---项目开发示例

    附加:在项目名称的右键后进入Properties然后进入Java Build Path后查看加载的jar包。 具体项目的开发详细文档请见下一篇文章《struts+hibernate+spring项目开发示例》 Myeclipse平台struts+hibernate+spring项目...

    .Advanced.Java.EE.Development.with.WildFly.1783288906

    Publication Date: 2015-03-27 ISBN-10: 1783288906 ISBN-13: 9781783288908 Your one-stop guide to developing Java® EE applications with the Eclipse IDE, Maven, and WildFly® 8.1 About This Book ...

    AHibernate1.1

    2.实现功能:自动处理java.util.Date类型. 3.实现功能:调试时自动输入sql到日志中,输出的sql已经将?参数替换为了传入的变量,sql能直接运行. 4.实现功能:实现了主键自增和不自增控制方法的重载.默认使用主键自增, //...

    java葵花宝典经典面试ppt

    常用的类:Array 包装类 BufferedReader BufferedWriter FileReader FileWirter String Integer java.util.Date System Class(反射的核心类) 实现集合接口类HashMap ArrayList LinkedList HashSet java.lang.String ...

    Hibernate自动生成数据库表映射实体类

    "import java.util.Calendar;\n\n"; str += "@Entity\n"; str += "@Table(name=\"$\")\n".replace("$", NameOfTable); str += "public class $ {\n".replace("$", NameOfTable.substring(2)); str += "\n @Id\n...

    Advanced.Java.EE.Development.with.WildFly.1783288906

    Publication Date: 2015-03-27 ISBN-10: 1783288906 ISBN-13: 9781783288908 Your one-stop guide to developing Java® EE applications with the Eclipse IDE, Maven, and WildFly® 8.1 About This Book ...

Global site tag (gtag.js) - Google Analytics