当前位置: 代码迷 >> Android >> java得到字符型的当前时间值
  详细解决方案

java得到字符型的当前时间值

热度:119   发布时间:2016-05-01 22:06:16.0
java得到字符型的当前时间值,在线等

java代码中,我要得到一个 String s= 当前时间值,

我要在System.out.println(s)时,可输出 (如 2012-02-21 80:00:00) 这样的值



这怎么实现呀,thanks

------解决方案--------------------
楼主,这个你取到系统当前时间值,然后装换成字符串啊 很简单的。自己解决吧,会提升自己的能力的。
------解决方案--------------------
Java code
public static String GetTimeByString()    {        Calendar c = Calendar.getInstance();        int second;        int minute;        int hour;        int day;        int month;        int year;        year = c.get(Calendar.YEAR);        month = c.get(Calendar.MONTH);        day = c.get(Calendar.DAY_OF_MONTH);        hour = c.get(Calendar.HOUR_OF_DAY);        minute = c.get(Calendar.MINUTE);        second = c.get(Calendar.SECOND);        String syear = String.valueOf(year);        String smonth;        String sday;        String shour;        String sminute;        String ssecond;        if(month < 10) smonth = "0" + month;        else smonth = String.valueOf(month);        if(day < 10) sday = "0" + day;        else sday = String.valueOf(day);        if(hour < 10) shour = "0" + hour;        else shour = String.valueOf(hour);        if(minute < 10) sminute = "0" + minute;        else sminute = String.valueOf(minute);        if(second < 10) ssecond = "0" + second;        else ssecond = String.valueOf(second);        String time = syear + "-" + smonth + "-" + sday    + " " + shour + ":" + sminute + ":" + ssecond;        return time;    }
------解决方案--------------------
Java code
 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");         String time=df.format(new Date());         System.out.println("当前的时间为:"+time);
------解决方案--------------------
3楼的是最简单的实现!
  相关解决方案