SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Calendar calendar  = Calendar.getInstance();
calendar.setTime(sdf.parse("2009-3-1 12:00:00"));
System.out.println(sdf.format(calendar.getTime()));
System.out.println(calendar.get(calendar.AM_PM));
System.out.println(calendar.get(calendar.HOUR_OF_DAY));我们我获取的时间会是0呢,我想得到12,有什么办法没有

解决方案 »

  1.   


    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    public class testDate {
    public static void main(String []args) throws ParseException{
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    Calendar calendar  = Calendar.getInstance(); 
    calendar.setTime(sdf.parse("2009-3-1 12:00:00")); 
    System.out.println(sdf.format(calendar.getTime())); 
    System.out.println(calendar.get(Calendar.AM_PM)); 
    System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); 
    }
    }
      

  2.   

    yyyy-MM-dd HH:mm:ss  大写HH才是24小时制的
      

  3.   


            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 12小时制
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 24小时制
      

  4.   

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
    Calendar calendar  = Calendar.getInstance(); 
    try {
    calendar.setTime(sdf.parse("2009-3-1 12:00:00"));
    } catch (ParseException e) {
    e.printStackTrace();
    }
    String a= sdf.format(calendar.getTime());
    System.out.println(a); 
    System.out.println(a.substring(11, 13).replace(":", "").trim());
      

  5.   


    public static void main(String[] args){
    //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//12小时制
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制
    Calendar calendar  = Calendar.getInstance();
    try {
    calendar.setTime(sdf.parse("2009-3-1 12:00:00"));
    } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    System.out.println(sdf.format(calendar.getTime()));
    //System.out.println(calendar.get(calendar.AM_PM));
    System.out.println(calendar.get(calendar.HOUR_OF_DAY)); 
    }
      

  6.   

    public String time()
    {
    Calendar c=Calendar.getInstance();
    return Integer.toString(c.get(Calendar.YEAR))+"-"+Integer.toString(c.get(Calendar.MONTH)+1)+
    "-"+Integer.toString(c.get(Calendar.DAY_OF_MONTH))+" "+Integer.toString(c.get(Calendar.HOUR_OF_DAY))+
    ":"+Integer.toString(c.get(Calendar.MINUTE))+":"+Integer.toString(c.get(Calendar.SECOND ));
    }