格式化任意的时间格式

 

None.gif
None.gif
import  java.util.Calendar;
None.gif
import  java.util. * ;
None.gif
import  java.sql.Timestamp;
None.gif
None.gif
public   class  TimeFormat
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
private static String[] keywords =
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockEnd.gif      
"year""month""day""hour""minute""second""millisecond"}
;
InBlock.gif  
private String format;
InBlock.gif  
private Calendar whatTime;
InBlock.gif
InBlock.gif  
public TimeFormat()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/** *//**
InBlock.gif   *
InBlock.gif   * 
@param format year-month-day hour:minute:second   2005-11-25 22:02
InBlock.gif   * 
@param timeStr
InBlock.gif   * 
@return
InBlock.gif   *
InBlock.gif   * year年month月day日 hour点minute分second秒
InBlock.gif   * 2005年11月25日 22点22分15秒
InBlock.gif   * 2005/11/25  22:22
InBlock.gif   * 05/11/25 22:22
ExpandedSubBlockEnd.gif   
*/

InBlock.gif  
public static Calendar getTime(String format, String timeVarStr)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    String timeFormatStr 
= format;
InBlock.gif    Calendar whatTime 
= null;
InBlock.gif    
int year = 0, month = 0, day = 0, hour = 0, minute = 0,
InBlock.gif        second 
= 0, millisecond = 0;
InBlock.gif
InBlock.gif    String[] validTimeElements 
=
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        
"year""month""day""hour""minute""second""millisecond"}
;
InBlock.gif
InBlock.gif    
int dateVarCount = 0;
InBlock.gif    
int index = -1;
InBlock.gif
InBlock.gif    Vector tmpV 
= new Vector();
InBlock.gif
InBlock.gif    Map map 
= new HashMap();
InBlock.gif
InBlock.gif    
for (int i = 0; i < validTimeElements.length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif       
int pos = format.indexOf(validTimeElements[i]);
InBlock.gif       
if(pos>=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif         map.put(
new Integer(pos),validTimeElements[i]);
ExpandedSubBlockEnd.gif       }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    Object[] tmp 
= map.keySet().toArray();
InBlock.gif    Integer[] tmpInt 
= new Integer[tmp.length];
InBlock.gif
InBlock.gif    
for(int i = 0; i<tmpInt.length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      tmpInt[i] 
= (Integer) tmp[i];
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//从小到大排序, 还原原来的顺序.
InBlock.gif
    Arrays.sort(tmpInt);
InBlock.gif    String[] varNames  
= new String[tmpInt.length];
InBlock.gif
InBlock.gif    
for(int i=0;i<varNames.length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      varNames[i] 
= (String) map.get(tmpInt[i]);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    String[] varValue 
= getTimeInOriginOrder(timeVarStr); //变量值
InBlock.gif

InBlock.gif    
if(varValue==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      System.out.println(
"获取时间变量的值出错了.");
InBlock.gif      
return null;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
int[] timeElementValue = new int[validTimeElements.length];
InBlock.gif
InBlock.gif    
for(int i=0;i<varNames.length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
for(int j=0;j<validTimeElements.length;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
if(varNames[i].equals(validTimeElements[j]))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif          timeElementValue[j] 
= Integer.parseInt(varValue[i]);
InBlock.gif          
break;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
if(format.indexOf("month")>=0 || format.indexOf("day")>=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
if(format.indexOf("year")<0)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        System.out.println(
"没有年, 使用当前的年为缺省值.");
InBlock.gif        timeElementValue[
0= Calendar.getInstance().get(Calendar.YEAR);
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif     
//目前禁止年份是非4位数字表示的
InBlock.gif
      String tmpStr = timeElementValue[0]+"";
InBlock.gif     
if(tmpStr.length()<4)
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif        System.out.println(
"目前禁止年份是非4位数字表示的,因为我们无法确定到底是哪一年");
InBlock.gif        
return null;
ExpandedSubBlockEnd.gif     }

InBlock.gif
InBlock.gif
InBlock.gif      whatTime 
=  Calendar.getInstance();
InBlock.gif      whatTime.set(timeElementValue[
0], timeElementValue[1- 1,
InBlock.gif                   timeElementValue[
2], timeElementValue[3], timeElementValue[4],
InBlock.gif                   timeElementValue[
5]);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      System.out.println(
"时间变量获取失败,至少必须又2个时间变量. year,month,day 必须都有, 可以允许year没有使用当前年份");
InBlock.gif      whatTime 
= null;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return whatTime;
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/** *//**
InBlock.gif   * 把时间变量的值按顺序取出,以逗号隔开
InBlock.gif   * 
@param val
InBlock.gif   * 
@return
ExpandedSubBlockEnd.gif   
*/

InBlock.gif  
static private String[] getTimeInOriginOrder(String val)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    String temp 
= "";
InBlock.gif    String digital 
= "0123456789";
InBlock.gif    
for (int i = 0; i < val.length(); i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
if (digital.indexOf(val.substring(i, i + 1)) > -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        temp 
= temp + val.substring(i, i + 1);
ExpandedSubBlockEnd.gif      }

InBlock.gif      
else
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
if (temp.length()>0 && temp.charAt(temp.length()-1)!=',' )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif          temp 
= temp + ",";
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    System.out.println(temp);
InBlock.gif    
return temp.split(","); //以逗号隔开的数组.
ExpandedSubBlockEnd.gif
  }

InBlock.gif
InBlock.gif  
public Calendar getTime(String timeStr)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    
return getTime(format, timeStr);
ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/** *//**year, month, day, hour, minute,second, millisecond
InBlock.gif      年    月     日    时   分     秒      毫秒
InBlock.gif     这里暂时假设年份都是显示4位数字,月分和天数都是显示不带前置0的形式,
InBlock.gif     如1998年9月3日,就不显示为98年09月03日,等日后完善。
InBlock.gif     如果有显示年必须指定年要显示多少位。
InBlock.gif     假设当前时间是:2002-11-15 20:13:55.359
InBlock.gif     用户设置: year年month月day日hour时minute分second秒
InBlock.gif     输出:2002年11月15日 20时13分55秒
InBlock.gif     用户设置: year年month月day日
InBlock.gif     输出:2002年11月15日
InBlock.gif   注意必须用小写的形式,大写的不行,因为用户可能在时间字符串内
InBlock.gif   也有大小写有区别的其他单词.
ExpandedSubBlockEnd.gif   *
*/

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/** *//**
InBlock.gif     如果正确 就返回其出现的位置,如果错误就返回-1.
ExpandedSubBlockEnd.gif   
*/

InBlock.gif
InBlock.gif  
public static int isTimestampValid(String timeStr, String element)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值