I'll be back!

  Focus on BPM, celebrate PegaRULES Process Commander (PRPC)
posts - 76, comments - 161, trackbacks - 0, articles - 2
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

JAVA中日期格式转换

Posted on 2006-09-25 10:34 zolly 阅读(9182) 评论(0)  编辑  收藏
/**
   * 字符串转换为java.util.Date<br>
   * 支持格式为 yyyy.MM.dd G 'at' hh:mm:ss z 如 '2002-1-1 AD at 22:10:59 PSD'<br>
   * yy/MM/dd HH:mm:ss 如 '2002/1/1 17:55:00'<br>
   * yy/MM/dd HH:mm:ss pm  如 '2002/1/1 17:55:00 pm'<br>
   * yy-MM-dd HH:mm:ss 如 '2002-1-1 17:55:00' <br>
   * yy-MM-dd HH:mm:ss am 如 '2002-1-1 17:55:00 am' <br>
   * 
@param  time String 字符串<br>
   * 
@return  Date 日期<br>
   
*/

  
public   static  Date stringToDate(String time) {
    SimpleDateFormat formatter;
    
int  tempPos = time.indexOf( " AD " ) ;
    time
= time.trim() ;
    formatter 
=   new  SimpleDateFormat ( " yyyy.MM.dd G 'at' hh:mm:ss z " );
    
if (tempPos >- 1 ) {
      time
= time.substring( 0 ,tempPos) +
           
" 公元 " + time.substring(tempPos + " AD " .length()); // china
      formatter  =   new  SimpleDateFormat ( " yyyy.MM.dd G 'at' hh:mm:ss z " );
    }

    tempPos
= time.indexOf( " - " );
    
if (tempPos >- 1 && (time.indexOf( "   " ) < 0 )) {
      formatter 
=   new  SimpleDateFormat ( " yyyyMMddHHmmssZ " );
    }

    
else   if ((time.indexOf( " / " ) >- 1 && (time.indexOf( "   " ) >- 1 )) {
      formatter 
=   new  SimpleDateFormat ( " yyyy/MM/dd HH:mm:ss " );
    }

    
else   if ((time.indexOf( " - " ) >- 1 && (time.indexOf( "   " ) >- 1 )) {
      formatter 
=   new  SimpleDateFormat ( " yyyy-MM-dd HH:mm:ss " );
    }

    
else   if ((time.indexOf( " / " ) >- 1 && (time.indexOf( " am " ) >- 1 || (time.indexOf( " pm " ) >- 1 )) {
      formatter 
=   new  SimpleDateFormat ( " yyyy-MM-dd KK:mm:ss a " );
    }

    
else   if ((time.indexOf( " - " ) >- 1 && (time.indexOf( " am " ) >- 1 || (time.indexOf( " pm " ) >- 1 )) {
      formatter 
=   new  SimpleDateFormat ( " yyyy-MM-dd KK:mm:ss a " );
    }

    ParsePosition pos 
=   new  ParsePosition( 0 );
    java.util.Date ctime 
=  formatter.parse(time, pos);

    
return  ctime;
  }

  
/**
   * 将java.util.Date 格式转换为字符串格式'yyyy-MM-dd HH:mm:ss'(24小时制)<br>
   * 如Sat May 11 17:24:21 CST 2002 to '2002-05-11 17:24:21'<br>
   * 
@param  time Date 日期<br>
   * 
@return  String   字符串<br>
   
*/

   

  
public   static  String dateToString(Date time) {
    SimpleDateFormat formatter;
    formatter 
=   new  SimpleDateFormat ( " yyyy-MM-dd HH:mm:ss " );
    String ctime 
=  formatter.format(time);

    
return  ctime;
  }

  
/**
   * 将java.util.Date 格式转换为字符串格式'yyyy-MM-dd HH:mm:ss a'(12小时制)<br>
   * 如Sat May 11 17:23:22 CST 2002 to '2002-05-11 05:23:22 下午'<br>
   * 
@param  time Date 日期<br>
   * 
@param  x int 任意整数如:1<br>
   * 
@return  String 字符串<br>
   
*/

  
public   static  String dateToString(Date time, int  x) {
    SimpleDateFormat formatter;
    formatter 
=   new  SimpleDateFormat ( " yyyy-MM-dd KK:mm:ss a " );
    String ctime 
=  formatter.format(time);

    
return  ctime;
  }

  
/**
   *取系统当前时间:返回只值为如下形式
   *2002-10-30 20:24:39
   * 
@return  String
   
*/

  
public   static  String Now() {
    
return  dateToString( new  Date());
  }


  
/**
   *取系统当前时间:返回只值为如下形式
   *2002-10-30 08:28:56 下午
   *
@param  hour 为任意整数
   *
@return  String
   
*/

  
public   static  String Now( int  hour) {
    
return  dateToString( new  Date(),hour);
  }

  
/**
   *取系统当前时间:返回值为如下形式
   *2002-10-30
   *
@return  String
   
*/

  
public   static  String getYYYY_MM_DD() {
    
return  dateToString( new  Date()).substring( 0 , 10 );

  }

  
/**
   *取系统给定时间:返回值为如下形式
   *2002-10-30
   *
@return  String
   
*/

   
public   static  String getYYYY_MM_DD(String date) {
    
return  date.substring( 0 , 10 );

  }


  
public   static  String getHour() {
    SimpleDateFormat formatter;
    formatter 
=   new  SimpleDateFormat ( " H " );
    String ctime 
=  formatter.format( new  Date());
    
return  ctime;
    }


  
public   static  String getDay() {
      SimpleDateFormat formatter;
    formatter 
=   new  SimpleDateFormat ( " d " );
    String ctime 
=  formatter.format( new  Date());
    
return  ctime;
    }


  
public   static  String getMonth() {
    SimpleDateFormat formatter;
    formatter 
=   new  SimpleDateFormat ( " M " );
    String ctime 
=  formatter.format( new  Date());
    
return  ctime;
    }

  

  
public   static  String getYear() {
    SimpleDateFormat formatter;
    formatter 
=   new  SimpleDateFormat ( " yyyy " );
    String ctime 
=  formatter.format( new  Date());
    
return  ctime;
    }

      
  
public   static  String getWeek() {
    SimpleDateFormat formatter;
    formatter 
=   new  SimpleDateFormat ( " E " );
    String ctime 
=  formatter.format( new  Date());
    
return  ctime;
    }
 

在jsp页面中的日期格式和sqlserver中的日期格式不一样,怎样统一?


在页面上显示输出时,用下面的函数处理一下

public class DateUtil(){
    
public static String fmtShortEnu(Date myDate) {
    SimpleDateFormat formatter 
= new SimpleDateFormat("yyyy/MM/dd");
    String strDate 
= formatter.format(myDate);
    
return strDate;
  }

}

new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
new java.text.SimpleDateFormat("yyyy-MM-dd")
建议还是把sqlserver的字段类型改成varchar的吧,用字符串处理可以完全按照自己的意愿处理,没有特殊的需求,不要使用date型


字串日期格式转换
用的API是SimpleDateFormat,它是属於java.text.SimpleDateFormat,所以请记得import进来!

用法:
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
这一行最重要,它确立了转换的格式,yyyy是完整的西元年,MM是月份,dd是日期, 至於HH:mm:ss就不需要我再解释了吧!
ps:为什麽有的格式大写,有的格式小写,那是怕避免混淆,例如MM是月份,mm是分;HH是24小时制,而hh是12小时制

1.字串转日期:
 2002-10-8 15:30:22要把它转成日期,可以用
 Date date=sdf.parse("2002-10-8 15:30:22");
2.日期转字串
 假如把今天的日期转成字串可用
 String datestr=sdf.format(new Date());
 这个字串的内容便类似2002-10-08 14:55:38

透过这个API我们便可以随心所欲的将日期转成我们想要的字串格式,例如希望将日期输出成2002年10月08日,
我们可以这麽写:
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
String datestr=sdf.format(new Date());
datestr便会依照我们设定的格式输出

//对日期格式的转换成("yyyy-MM-dd")格式的方法
public java.sql.Date Convert(String str)
  
{
    java.text.SimpleDateFormat sdf 
= new java.text.SimpleDateFormat("yyyy-MM-dd");
    
try
    
{
      java.util.Date d 
= sdf.parse(str);
      java.sql.Date d1 
= new java.sql.Date(d.getTime());
      
return d1;
    }

    
catch(Exception ex)
    
{
      ex.printStackTrace();
      
return null;
    }

  }


应用如下:
ctmt.setDate(7,this.Convert(info.getManBirth()));  // @DATETIME

获得本月一日、本星期星期一、昨天的date对象的方法:(摘自:http://www.blogjava.net/qclass/archive/2006/09/24/71589.html)

        GregorianCalendar cal  =   new  GregorianCalendar();
        Date now  
=   new  Date();
        cal.setTime(now);
        cal.setFirstDayOfWeek(GregorianCalendar.MONDAY);   
// 设置一个星期的第一天为星期1,默认是星期日 
        SimpleDateFormat dateutil  =   new  SimpleDateFormat( " yyyy-MM-dd " );
        System.out.println( 
" now= " + dateutil.format(cal.getTime()));  // 今天 
        
        cal.add(GregorianCalendar.DATE, 
- 1 );
        System.out.println( 
" now= " + dateutil.format(cal.getTime())); // 昨天         
        
        cal.set(GregorianCalendar.DAY_OF_WEEK, GregorianCalendar.MONDAY);
        System.out.println( 
" now= " + dateutil.format(cal.getTime()));   // 本周1 

        cal.set(GregorianCalendar.DAY_OF_MONTH,  
1 ); 
        System.out.println( 
" now= " + dateutil.format(cal.getTime())); // 本月1日 


摘自:http://www.blogjava.net/galaxyp/archive/2006/04/13/40826.html

只有注册用户登录后才能发表评论。


网站导航: