欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

【Java】Java中获取时间的方法,

来源: javaer 分享于  点击 21252 次 点评:47

【Java】Java中获取时间的方法,


import java.text.*;
import java.util.*;
public final class Constants ...{    // Session keys    
        public static final String getDate()...{        
               Calendar cal 
= Calendar.getInstance();        
               java.text.SimpleDateFormat sdf 
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        
              String cdate 
= sdf.format(cal.getTime());                
              System.out.println(
"times is :"+cdate);        
              
return cdate;     
        }

}

上面的代码用到了Java中的Calendar类,这是个时间类,后面用到了时间格式化的类。
“yyyy-MM-dd HH:mm:ss”代表要想得到的时间字符串的类型。

String cdate = sdf.format(cal.getTime());     

通过格式化类的format方法格式化时间,得到想要的字符串。

相关文章

    暂无相关文章
相关栏目:

用户点评