時間格式化最全的類,拷進工程就可使用

/******************************************************************************
 * @File name   :      DataFormat.java
 *
 * @Author      :      licheng
 *
 * @Date        :      2012-1-16
 *
 * @Copyright Notice:
 * Copyright (c) 2012 Haier, Inc. All  Rights Reserved.
 * This software is published under the terms of the Haier Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 *
 *
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2012-1-16 下午4:15:29        licheng     1.0            Initial Version
 *****************************************************************************/
package com.haier.hmms.util;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

/**
 *
 */
public class DataFormat {


 /** 已定義的日期格式常量 */
 public static final int DT_YYYYMMDD = 1;
 public static final int DT_YYYYMMDD_HHMMSS = 2;
 public static final int DT_HHMMSS = 3;
 public static final int DT_HHMM = 4;
 public static final int DT_YYYY = 5;
 public static final int DT_MMDDYYYYHHMMSS = 6;
 public static final int DT_YYYYMMDD_EEEE = 7; // ex:2007-06-15 星期五
 public static final int DT_YYMMDD = 8; // ex:070602
 public static final int DT_YYYYMM = 9;
 public static final int DT_YYYYMMDD2 = 10;
 public static final int DT_HHMMSS1 = 11;
 public static final int DT_MMDDYYYYHHMMSS1 = 12;

 /**
  * 格式化數字,例如:12345轉化爲12345
  *
  * @param dValue
  *            被格式化的數值
  * @param iScale
  *            小數點後保留位數,不足補0
  * @return
  */
 public static String formatNumberToString(double dValue, int iScale) {
  if (Double.isNaN(dValue)) {
   return "";
  }

  DecimalFormat df = null;
  StringBuffer sPattern = new StringBuffer("##0");
  if (iScale > 0) {
   sPattern.append(".");
   for (int i = 0; i < iScale; i++) {
    sPattern.append("0");
   }
  }
  df = new DecimalFormat(sPattern.toString());
  return df.format(dValue);
 }

 /**
  * 格式化數字,例如:12345轉化爲12,345
  *
  * @param dValue
  *            被格式化的數值
  * @param iScale
  *            小數點後保留位數,不足補0
  * @return
  */
 public static String formatNumber(double dValue, int iScale) {
  if (Double.isNaN(dValue)) {
   return "";
  }

  DecimalFormat df = null;
  StringBuffer sPattern = new StringBuffer(",##0");
  if (iScale > 0) {
   sPattern.append(".");
   for (int i = 0; i < iScale; i++) {
    sPattern.append("0");
   }
  }
  df = new DecimalFormat(sPattern.toString());
  return df.format(dValue);
 }

 /**
  * 該方法不能對數據庫中的id值做轉換,當id>1000時會出現以下問題 id=12000時,該方法返回12,000
  */
 public static String formatNumber(long lValue) {
  return formatNumber((double) lValue, 0);
 }

 /**
  * 解析格式化的字符串,轉化爲數值,例如:12,345.00轉化爲12345
  *
  * @param text
  *            被格式化的數值
  * @return
  */
 public static double parseDouble(String text)
 {
  String localText = text;
  int index = localText.indexOf(",");
  StringBuffer sbNumber = new StringBuffer("");
  while (index != -1)
  {
   sbNumber.append(localText.substring(0 ,index));
   localText = localText.substring (index + 1 ,localText.length());
   index = localText.indexOf(",");
  }
  sbNumber.append(localText);
  return Double.parseDouble(sbNumber.toString());
 }

 public static long parseLong(String text) {
  String localText = text;
  int index = localText.indexOf(",");
  StringBuffer sbNumber = new StringBuffer("");
  while (index != -1) {
   sbNumber.append(localText.substring(0 ,index));
   localText = localText.substring(index + 1, localText.length());
   index = localText.indexOf(",");
  }
  sbNumber.append(localText);
  return Long.parseLong(sbNumber.toString());
 }

 /**
  * 按指定的模板獲得對應的時間串
  *
  * @param date
  * @param nFmt
  * @return
  */
 public static String formatDate(Date date, int nFmt) {
  if (date == null) {
   return "";
  }
  SimpleDateFormat dateFormat = new SimpleDateFormat();
  switch (nFmt) {
  default:
  case DataFormat.DT_YYYYMMDD:
   dateFormat.applyPattern("yyyy-MM-dd");
   break;
  case DataFormat.DT_YYYYMMDD_HHMMSS:
   dateFormat.applyPattern("yyyy-MM-dd HH:mm:ss");
   break;
  case DataFormat.DT_HHMMSS:
   dateFormat.applyPattern("HH:mm:ss");
   break;
  case DataFormat.DT_HHMM:
   dateFormat.applyPattern("HH:mm");
   break;
  case DataFormat.DT_YYYY:
   dateFormat.applyPattern("yyyy");
   break;
  case DataFormat.DT_MMDDYYYYHHMMSS:
   dateFormat.applyPattern("MMddyyyy:HH:mm:ss");
   break;
  case DataFormat.DT_YYYYMMDD_EEEE:
   dateFormat.applyPattern("yyyy-MM-dd EEEE");
   break;
  case DataFormat.DT_YYMMDD:
   dateFormat.applyPattern("yyMMdd");
   break;
  case DataFormat.DT_YYYYMM:
   dateFormat.applyPattern("yyyyMM");
   break;
  case DataFormat.DT_YYYYMMDD2:
   dateFormat.applyPattern("yyyyMMdd");
   break;
  case DataFormat.DT_HHMMSS1:
   dateFormat.applyPattern("HHmmss");
   break;
  case DataFormat.DT_MMDDYYYYHHMMSS1:
   dateFormat.applyPattern("yyyyMMddHHmmss");
   break;
  }
  return dateFormat.format(date);
 }

 public static String formatDate(Date date) {
  return formatDate(date, DT_YYYYMMDD);
 }

 public static String formatDate(Date date, String strFmt) {
  if (date == null) {
   return "";
  }
  SimpleDateFormat dateFormat = new SimpleDateFormat(strFmt);
  return dateFormat.format(date);
 }

 /**
  * 按指定的模板轉換爲相應的時間
  *
  * @param strDate
  * @param nFmt
  * @return
  * @throws Exception
  */
 public static Date parseDate(String strDate, int nFmt) throws Exception {
  SimpleDateFormat dateFormat = new SimpleDateFormat();
  switch (nFmt) {
  case DataFormat.DT_YYYYMMDD:
   dateFormat.applyPattern("yyyy-MM-dd");
   break;
  case DataFormat.DT_YYYYMMDD_HHMMSS:
   dateFormat.applyPattern("yyyy-MM-dd HH:mm:ss");
   break;
  case DataFormat.DT_HHMMSS:
   dateFormat.applyPattern("HH:mm:ss");
   break;
  case DataFormat.DT_HHMM:
   dateFormat.applyPattern("HH:mm");
   break;
  default:
  }
  return dateFormat.parse(strDate);
 }

 public static Date parseDate(String strDate, String strFmt)
   throws Exception {
  SimpleDateFormat dateFormat = new SimpleDateFormat(strFmt);
  return dateFormat.parse(strDate);
 }

 /**
  * 按指定的分隔符分隔字符串<br>
  * 完成類似JDK1.4 String::split的功能
  *
  * @param1 splitedStr 需要被分割的String
  * @param2 splitConditon 分割的條件字符
  * @return String[] 分隔後的數組
  */
 public static String[] splitString(String splitedStr, String splitConditon) {
  int start = 0;
  int end = 0;
  ArrayList<String> list = new ArrayList<String>();
  String tmp = null;
  while (true) {
   if (start == splitedStr.length()) {
    break;
   }

   end = splitedStr.indexOf(splitConditon, start);
   if (end == -1) {
    tmp = splitedStr.substring(start, splitedStr.length());
    list.add(tmp);
    break;
   }
   if (start != end) {
    tmp = splitedStr.substring(start, end);
    list.add(tmp);
   }
   start = end + splitConditon.length();
  }
  String[] res = new String[list.size()];
  for (int i = 0; i < list.size(); i++) {
   res[i] = (String) list.get(i);
  }
  return res;
 }

 /**
  * 
  */
 public static String formatString(String strData) {
  if (strData == null || strData.trim().length() <= 0) {
   return "";
  } else {
   return strData.trim();
  }
 }

 public static String formatStringForHtml(String strData) {
  if (strData == null || strData.trim().length() <= 0) {
   return "&nbsp;";
  } else {
   return strData.trim();
  }
 }

 /**
  * 判斷字符串非空(不爲null,且不是空字符串)
  *
  * @param strInput
  * @return
  */
 public static boolean isNotBlank(String strInput) {
  return !isBlank(strInput);
 }

 /**
  * 判斷字符串爲空(爲null,或是空字符串)
  *
  * @param strInput
  * @return
  */
 public static boolean isBlank(String strInput) {
  if (strInput == null || strInput.trim().length() <= 0) {
   return true;
  }
  return false;
 }

 /**
  * 判斷dataDate是否等於當日
  * @Date        :      2012-6-9
  * @param dataDate
  * @return
  */
 public static boolean isToday(Date dataDate) {
  String strDataDate = formatDate(dataDate,"yyyy-MM-dd");
  String strToday = formatDate(new Date(),"yyyy-MM-dd");
  return strToday.equals(strDataDate);
 }
 
 /**
  * 把a,b,c類型字符串改爲'a','b','c'
  * @param str
  * @return
  */
 public static String formatSqlstr(String str){
  String news = "";
  if(StringUtils.isBlank(str)){
   return news;
  }
  String s[] = str.split(",");
  if(s.length>0){
   StringBuffer sb = new StringBuffer();
   for (int i = 0; i < s.length; i++) {
    sb.append("'");
    sb.append(s[i]);
    sb.append("',");
   }
   news = sb.toString().substring(0,sb.toString().length()-1);
  }
  return news;
 };
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章