有這樣一個字串:"20070911121547",
轉換成時間格式:2007-09-11 12:15:47
Java代碼
- public class bb {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
- String dateString = "20071128175545";
- try {
- Date date = df.parse(dateString);
- System.out.println(df.format(date));
- } catch (Exception ex) {
- System.out.println(ex.getMessage());
- }
- }
- }
public class bb { public static void main(String[] args) { // TODO Auto-generated method stub SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss"); String dateString = "20071128175545"; try { Date date = df.parse(dateString); System.out.println(df.format(date)); } catch (Exception ex) { System.out.println(ex.getMessage()); } }}
時間無非就是字串類型轉向時間類型,或則時間類型轉向字串類型,還有就是前一個時間,後一個時間的處理等等
自己做了一個例子:
Java代碼
- package com.observe.monitoralarm.util;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- /**
- * 時間協助類
- * @version $Id: DateUtil.java,v 1.1 2008/05/28 04:29:52 linan Exp $
- * @author LiNan
- */
- public class DateUtil {
- private Calendar calendar=Calendar.getInstance();
- /**
- * 得到當前的時間,時間格式yyyy-MM-dd
- * @return
- */
- public String getCurrentDate(){
- SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
- return sdf.format(new Date());
- }
- /**
- * 得到當前的時間,自訂時間格式
- * y 年 M 月 d 日 H 時 m 分 s 秒
- * @param dateFormat 輸出顯示的時間格式
- * @return
- */
- public String getCurrentDate(String dateFormat){
- SimpleDateFormat sdf=new SimpleDateFormat(dateFormat);
- return sdf.format(new Date());
- }
- /**
- * 日期格式化,預設日期格式yyyy-MM-dd
- * @param date
- * @return
- */
- public String getFormatDate(Date date){
- SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
- return sdf.format(date);
- }
- /**
- * 日期格式化,自訂輸出日期格式
- * @param date
- * @return
- */
- public String getFormatDate(Date date,String dateFormat){
- SimpleDateFormat sdf=new SimpleDateFormat(dateFormat);
- return sdf.format(date);
- }
- /**
- * 返回當前日期的前一個時間日期,amount為正數 目前時間後的時間 為負數 目前時間前的時間
- * 預設日期格式yyyy-MM-dd
- * @param field 日曆欄位
- * y 年 M 月 d 日 H 時 m 分 s 秒
- * @param amount 數量
- * @return 一個日期
- */
- public String getPreDate(String field,int amount){
- calendar.setTime(new Date());
- if(field!=null&&!field.equals("")){
- if(field.equals("y")){
- calendar.add(calendar.YEAR, amount);
- }else if(field.equals("M")){
- calendar.add(calendar.MONTH, amount);
- }else if(field.equals("d")){
- calendar.add(calendar.DAY_OF_MONTH, amount);
- }else if(field.equals("H")){
- calendar.add(calendar.HOUR, amount);
- }
- }else{
- return null;
- }
- return getFormatDate(calendar.getTime());
- }
- /**
- * 某一個日期的前一個日期
- * @param d,某一個日期
- * @param field 日曆欄位
- * y 年 M 月 d 日 H 時 m 分 s 秒
- * @param amount 數量
- * @return 一個日期
- */
- public String getPreDate(Date d,String field,int amount){
- calendar.setTime(d);
- if(field!=null&&!field.equals("")){
- if(field.equals("y")){
- calendar.add(calendar.YEAR, amount);
- }else if(field.equals("M")){
- calendar.add(calendar.MONTH, amount);
- }else if(field.equals("d")){
- calendar.add(calendar.DAY_OF_MONTH, amount);
- }else if(field.equals("H")){
- calendar.add(calendar.HOUR, amount);
- }
- }else{
- return null;
- }
- return getFormatDate(calendar.getTime());
- }
- /**
- * 某一個時間的前一個時間
- * @param date
- * @return
- * @throws ParseException
- */
- public String getPreDate(String date) throws ParseException{
- Date d=new SimpleDateFormat().parse(date);
- String preD=getPreDate(d,"d",1);
- Date preDate=new SimpleDateFormat().parse(preD);
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- return sdf.format(preDate);
- }
- }