日期:2014-05-20  浏览次数:20907 次

新手求解 java如何获取当前时间,最好语句少一点的
public static void main(String[] args) {
  Calendar now=Calendar.getInstance(); 
  int month = now.get(Calendar.MONTH) + 1;
  String m = ""; //
   
  if(month < 10) { //
  m = "0"+month;
  }
   
  String time = now.get(Calendar.YEAR)+ m + now.get(Calendar.DAY_OF_MONTH) + 
  now.get(Calendar.HOUR_OF_DAY)+now.get(Calendar.MINUTE)+
  now.get(Calendar.SECOND)+now.get(Calendar.MILLISECOND);
  System.out.println(time);

  }
  这里的m是干嘛的 求注释

------解决方案--------------------
DateFormat dateFormat = new SimpleDateFormate("yyyy-MM-dd HH:mm:ss.SSS");
System.out.println(dateFormat.format(new Date()));
------解决方案--------------------
Java code

System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
//2011-10-19

------解决方案--------------------
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
------解决方案--------------------
只要当前时间是么,一句就够了:

System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));


------解决方案--------------------
这是源代码:
Java code


import java.util.Date;
import java.text.DateFormat;
import java.util.Locale;
public class  DateDemo02
{
    public static void main(String[] args) 
    {
        DateFormat df1 = DateFormat.getDateInstance(DateFormat.YEAR_FIELD,new Locale("zh","CH"));
        DateFormat df2 = DateFormat.getDateTimeInstance(DateFormat.YEAR_FIELD,DateFormat.ERA_FIELD,new Locale("zh","CH"));
        System.out.println(df1.format(new Date()));
        System.out.println(df2.format(new Date()));
    }
}

------解决方案--------------------
探讨
DateFormat dateFormat = new SimpleDateFormate("yyyy-MM-dd HH:mm:ss.SSS");
System.out.println(dateFormat.format(new Date()));

------解决方案--------------------
DateFormat dateFormat = new SimpleDateFormate("yyyy-MM-dd HH:mm:ss.SSS");
System.out.println(dateFormat.format(new Date()));
一般需要导入import java.util.Date; import java.text.DateFormat;

------解决方案--------------------
Java code
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));