123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- package com.sinosoft.cm.common;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import nl.justobjects.pushlet.util.Sys;
- /**
- * 时间格式转换类
- * @author Administrator
- *
- */
- public class DateUtil {
- private static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- private static SimpleDateFormat formatterTime = new SimpleDateFormat("HH:mm");
- public DateUtil() {
- }
- /**
- * util格式date想sql格式date转换
- * @param date
- * @return
- */
- public static java.sql.Date toSqlDate(java.util.Date date){
- return new java.sql.Date(date.getTime());
-
- }
- /**
- * sql格式date向util格式date转换
- * @param date
- * @return
- */
- public static java.util.Date toUtildate(java.sql.Date date){
- return new java.util.Date(date.getTime());
- }
- /**
- * sql格式date转换为字符串
- * @param date
- * @return
- */
- public static String toString(java.sql.Date date){
- return toString(toUtildate(date));
- }
- /**
- * 字符串转换为sql格式date
- * @param dateString
- * @return
- */
- public static java.sql.Date toSqlDate(String dateString){
- return toSqlDate(toData(dateString));
- }
- /**
- * util格式date转换为字符串
- * @param date
- * @return
- */
- public static String toString(java.util.Date date){
- return formatter.format(date);
-
- }
-
- public static String toTimeString(java.util.Date date){
- return formatterTime.format(date);
-
- }
- /**
- * 字符串转换为util格式date
- * @param dateString
- * @return
- */
- public static java.util.Date toData(String dateString){
- try {
- return formatter.parse(dateString);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * 获取间隔时间集合
- * @param dateTime
- * @param n
- * @param timeInterval
- * @return
- */
- public static List<String> getUitlDates(String dateTime,int n,int timeInterval){
- List<String> dateString=new ArrayList<String>();
- for(int index=0;index<n;index++){
- Date temDate=DateUtil.toData(dateTime);
- temDate.setSeconds(DateUtil.toData(dateTime).getSeconds()-(n-index-1)*timeInterval);
- dateString.add(formatter.format(temDate));
- }
- System.out.println(dateString);
- return dateString;
- }
-
- public static List<String> dateStringToDateString(List<String> list){
- List<String> dateList=new ArrayList<String>();
- for(String item : list){
- Date temDate=DateUtil.toData(item);
- dateList.add(formatterTime.format(temDate));
- }
- return dateList;
- }
- /* public static void main(String[] args) {
- System.out.println(
- DateUtil.dateStringToDateString(DateUtil.getUitlDates("2012-12-12 13:12:12", 10, 3)));
- }*/
- }
|