java 日期API的应用

阅读更多
代码
  1. /**  
  2.    * 字符串转换为java.util.Date
     
  3.    * 支持格式为 yyyy.MM.dd G 'at' hh:mm:ss z 如 '2002-1-1 AD at 22:10:59 PSD'
     
  4.    * yy/MM/dd HH:mm:ss 如 '2002/1/1 17:55:00'
     
  5.    * yy/MM/dd HH:mm:ss pm  如 '2002/1/1 17:55:00 pm'
     
  6.    * yy-MM-dd HH:mm:ss 如 '2002-1-1 17:55:00' 
     
  7.    * yy-MM-dd HH:mm:ss am 如 '2002-1-1 17:55:00 am' 
     
  8.    * @param time String 字符串
     
  9.    * @return Date 日期
     
  10.    */  
  11.   public static Date stringToDate(String time){   
  12.     SimpleDateFormat formatter;   
  13.     int tempPos=time.indexOf("AD") ;   
  14.     time=time.trim() ;   
  15.     formatter = new SimpleDateFormat ("yyyy.MM.dd G 'at' hh:mm:ss z");   
  16.     if(tempPos>-1){   
  17.       time=time.substring(0,tempPos)+   
  18.            "公元"+time.substring(tempPos+"AD".length());//china   
  19.       formatter = new SimpleDateFormat ("yyyy.MM.dd G 'at' hh:mm:ss z");   
  20.     }   
  21.     tempPos=time.indexOf("-");   
  22.     if(tempPos>-1&&(time.indexOf(" ")<0)){   
  23.       formatter = new SimpleDateFormat ("yyyyMMddHHmmssZ");   
  24.     }   
  25.     else if((time.indexOf("/")>-1) &&(time.indexOf(" ")>-1)){   
  26.       formatter = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss");   
  27.     }   
  28.     else if((time.indexOf("-")>-1) &&(time.indexOf(" ")>-1)){   
  29.       formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");   
  30.     }   
  31.     else if((time.indexOf("/")>-1) &&(time.indexOf("am")>-1) ||(time.indexOf("pm")>-1)){   
  32.       formatter = new SimpleDateFormat ("yyyy-MM-dd KK:mm:ss a");   
  33.     }   
  34.     else if((time.indexOf("-")>-1) &&(time.indexOf("am")>-1) ||(time.indexOf("pm")>-1)){   
  35.       formatter = new SimpleDateFormat ("yyyy-MM-dd KK:mm:ss a");   
  36.     }   
  37.     ParsePosition pos = new ParsePosition(0);   
  38.     java.util.Date ctime = formatter.parse(time, pos);   
  39.   
  40.     return ctime;   
  41.   }   
  42.   /**  
  43.    * 将java.util.Date 格式转换为字符串格式'yyyy-MM-dd HH:mm:ss'(24小时制)
     
  44.    * 如Sat May 11 17:24:21 CST 2002 to '2002-05-11 17:24:21'
     
  45.    * @param time Date 日期
     
  46.    * @return String   字符串
     
  47.    */  
  48.       
  49.   
  50.   public static String dateToString(Date time){   
  51.     SimpleDateFormat formatter;   
  52.     formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");   
  53.     String ctime = formatter.format(time);   
  54.   
  55.     return ctime;   
  56.   }   
  57.   /**  
  58.    * 将java.util.Date 格式转换为字符串格式'yyyy-MM-dd HH:mm:ss a'(12小时制)
     
  59.    * 如Sat May 11 17:23:22 CST 2002 to '2002-05-11 05:23:22 下午'
     
  60.    * @param time Date 日期
     
  61.    * @param x int 任意整数如:1
     
  62.    * @return String 字符串
     
  63.    */  
  64.   public static String dateToString(Date time,int x){   
  65.     SimpleDateFormat formatter;   
  66.     formatter = new SimpleDateFormat ("yyyy-MM-dd KK:mm:ss a");   
  67.     String ctime = formatter.format(time);   
  68.   
  69.     return ctime;   
  70.   }   
  71.   /**  
  72.    *取系统当前时间:返回只值为如下形式  
  73.    *2002-10-30 20:24:39  
  74.    * @return String  
  75.    */  
  76.   public static String Now(){   
  77.     return dateToString(new Date());   
  78.   }   
  79.   
  80.   /**  
  81.    *取系统当前时间:返回只值为如下形式  
  82.    *2002-10-30 08:28:56 下午  
  83.    *@param hour 为任意整数  
  84.    *@return String  
  85.    */  
  86.   public static String Now(int hour){   
  87.     return dateToString(new Date(),hour);   
  88.   }   
  89.   /**  
  90.    *取系统当前时间:返回值为如下形式  
  91.    *2002-10-30  
  92.    *@return String  
  93.    */  
  94.   public static String getYYYY_MM_DD(){   
  95.     return dateToString(new Date()).substring(0,10);   
  96.   
  97.   }   
  98.   /**  
  99.    *取系统给定时间:返回值为如下形式  
  100.    *2002-10-30  
  101.    *@return String  
  102.    */  
  103.    public static String getYYYY_MM_DD(String date){   
  104.     return date.substring(0,10);   
  105.   
  106.   }   
  107.   
  108.   public static String getHour(){   
  109.     SimpleDateFormat formatter;   
  110.     formatter = new SimpleDateFormat ("H");   
  111.     String ctime = formatter.format(new Date());   
  112.     return ctime;   
  113.     }   
  114.   
  115.   public static String getDay(){   
  116.       SimpleDateFormat formatter;   
  117.     formatter = new SimpleDateFormat ("d");   
  118.     String ctime = formatter.format(new Date());   
  119.     return ctime;   
  120.     }   
  121.   
  122.   public static String getMonth(){   
  123.     SimpleDateFormat formatter;   
  124.     formatter = new SimpleDateFormat ("M");   
  125.     String ctime = formatter.format(new Date());   
  126.     return ctime;   
  127.     }   
  128.      
  129.   
  130.   public static String getYear(){   
  131.     SimpleDateFormat formatter;   
  132.     formatter = new SimpleDateFormat ("yyyy");   
  133.     String ctime = formatter.format(new Date());   
  134.     return ctime;   
  135.     }   
  136.          
  137.   public static String getWeek(){   
  138.     SimpleDateFormat formatter;   
  139.     formatter = new SimpleDateFormat ("E");   
  140.     String ctime = formatter.format(new Date());   
  141.     return ctime;   
  142.     }    
  143.   
  144. 在jsp页面中的日期格式和sqlserver中的日期格式不一样,怎样统一?   
  145.   
  146. 在页面上显示输出时,用下面的函数处理一下   
  147.   
  148. public class DateUtil(){   
  149.     public static String fmtShortEnu(Date myDate) {   
  150.     SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");   
  151.     String strDate = formatter.format(myDate);   
  152.     return strDate;   
  153.   }   
  154. }   
  155.   
  156. new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
  157. new java.text.SimpleDateFormat("yyyy-MM-dd")   
  158. 建议还是把sqlserver的字段类型改成varchar的吧,用字符串处理可以完全按照自己的意愿处理,没有特殊的需求,不要使用date型   
  159.   
  160.   
  161. 字串日期格式转换    
  162. 用的API是SimpleDateFormat,它是属於java.text.SimpleDateFormat,所以请记得import进来!    
  163.   
  164. 用法:    
  165. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    
  166. 这一行最重要,它确立了转换的格式,yyyy是完整的西元年,MM是月份,dd是日期, 至於HH:mm:ss就不需要我再解释了吧!    
  167. ps:为什麽有的格式大写,有的格式小写,那是怕避免混淆,例如MM是月份,mm是分;HH是24小时制,而hh是12小时制    
  168.   
  169. 1.字串转日期:    
  170.  2002-10-8 15:30:22要把它转成日期,可以用    
  171.  Date date=sdf.parse("2002-10-8 15:30:22");    
  172. 2.日期转字串    
  173.  假如把今天的日期转成字串可用    
  174.  String datestr=sdf.format(new Date());    
  175.  这个字串的内容便类似2002-10-08 14:55:38    
  176.   
  177. 透过这个API我们便可以随心所欲的将日期转成我们想要的字串格式,例如希望将日期输出成2002年10月08日,    
  178. 我们可以这麽写:    
  179. SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");    
  180. String datestr=sdf.format(new Date());    
  181. datestr便会依照我们设定的格式输出   
  182.   
  183.   
  184. //对日期格式的转换成("yyyy-MM-dd")格式的方法   
  185. public java.sql.Date Convert(String str)   
  186.   {   
  187.     java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");   
  188.     try  
  189.     {   
  190.       java.util.Date d = sdf.parse(str);   
  191.       java.sql.Date d1 = new java.sql.Date(d.getTime());   
  192.       return d1;   
  193.     }   
  194.     catch(Exception ex)   
  195.     {   
  196.       ex.printStackTrace();   
  197.       return null;   
  198.     }   
  199.   }   
  200. 应用如下:   
  201. ctmt.setDate(7,this.Convert(info.getManBirth()));  // @DATETIME   

你可能感兴趣的:(Java,SQL,IDEA,JSP)