日期、时间Date/Time实例

如何显示AM-PM格式格式化时间?

package com.xyc.formatInstance;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 
 * @ClassName:  Main   
 * @Description:如何显示AM-PM格式格式化时间?
 * @author: xyc 
 * @date:   2017年2月10日 下午3:35:36   
 *
 */
public class DisplayTime{
    
   public static void main(String[] args){
       
      Date date = new Date();
      String strDateFormat = "HH:mm:ss a";
      SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
      
      System.out.println(sdf.format(date));
      
   }
}
View Code

上面的代码示例将产生以下结果:

16:28:37 下午

  

如何以MMMM格式,格式化时间?  

package com.xyc.formatInstance;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * 
 * @ClassName:  Main   
 * @Description:如何以MMMM格式,格式化时间?  
 * @author: xyc 
 * @date:   2017年2月10日 下午3:53:13   
 *
 */
public class FormatMonth{
   public static void main(String[] args){
       
      Date date = new Date();
      SimpleDateFormat sdf = new SimpleDateFormat("MMMM");
      System.out.println("Current Month in MMMM format : " + sdf.format(date));
   }
}
View Code

上面的代码示例将产生以下结果:

Current Month in MMMM format : 二月

 

如何显示小时和分钟(当前时间)?

package com.xyc.formatInstance;

import java.util.Calendar;
import java.util.Formatter;

/**
 * 
 * @ClassName:  MainClass   
 * @Description:如何显示小时和分钟(当前时间)?
 * @author: xyc 
 * @date:   2017年2月10日 下午3:45:12   
 *
 */
public class DisplayHourAndMinute {

    public static void main(String args[]) {

        Formatter fmt = new Formatter();
        Calendar cal = Calendar.getInstance();
        fmt = new Formatter();
        fmt.format("%tl:%tM", cal, cal);
        System.out.println(fmt);

    }
}
View Code

上面的代码示例将产生以下结果:

4:30

 

如何以24小时制格式化时间?

package com.xyc.formatInstance;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 
 * @ClassName:  Main   
 * @Description:如何以24小时制格式化时间?
 * @author: xyc 
 * @date:   2017年2月10日 下午3:50:35   
 *
 */
public class Format24Hour {
    
   public static void main(String[] args) {
       
      Date date = new Date();
      SimpleDateFormat sdf = new SimpleDateFormat("h");
      System.out.println("hour in h format : "+ sdf.format(date));
      
   }
}
View Code

上面的代码示例将产生以下结果:

hour in h format : 4

  

如何以(MMM)格式显示月份的名称?

package com.xyc.formatInstance;
import java.util.Calendar;
import java.util.Formatter;

/**
 * 
 * @ClassName:  MainClass   
 * @Description:如何以(MMM)格式显示月份的名称?
 * @author: xyc 
 * @date:   2017年2月10日 下午3:38:20   
 *
 */
public class GetMonth{
    
   public static void main(String args[]){
       
      Formatter fmt = new Formatter();
      Calendar cal = Calendar.getInstance();
      fmt = new Formatter();
      fmt.format("%tB %tb %tm", cal, cal, cal);
      System.out.println(fmt);
      
   }
}
View Code

上面的代码示例将产生以下结果:

二月 二月 02

 

如何格式化秒?

package com.xyc.formatInstance;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 
 * @ClassName:  Main   
 * @Description:如何格式化秒?
 * @author: xyc 
 * @date:   2017年2月10日 下午3:54:02   
 *
 */
public class FormatSecond{
    
   public static void main(String[] args){
       
      Date date = new Date();
      SimpleDateFormat sdf = new SimpleDateFormat("ss");
      System.out.println("seconds in ss format : " + sdf.format(date));
   }
}
View Code

上面的代码示例将产生以下结果:

seconds in ss format : 30

  

如何显示短格式的月份名称?

package com.xyc.formatInstance;
import java.text.DateFormatSymbols;

/**
 * 
 * @ClassName:  Main   
 * @Description:如何显示短格式的月份名称?
 * @author: xyc 
 * @date:   2017年2月10日 下午3:56:29   
 *
 */
public class DisPlayShortMonth {
    
   public static void main(String[] args) {
       
      String[] shortMonths = new DateFormatSymbols().getShortMonths();
      
      for (int i = 0; i < (shortMonths.length-1); i++) {
         String shortMonth = shortMonths[i];
         System.out.println("shortMonth = " + shortMonth);
      }
   }
}
View Code

上面的代码示例将产生以下结果:

shortMonth = 一月
shortMonth = 二月
shortMonth = 三月
shortMonth = 四月
shortMonth = 五月
shortMonth = 六月
shortMonth = 七月
shortMonth = 八月
shortMonth = 九月
shortMonth = 十月
shortMonth = 十一月
shortMonth = 十二月

  

如何显示在星期一至五的名称?

package com.xyc.formatInstance;
import java.text.DateFormatSymbols;

/**
 * 
 * @ClassName:  Main   
 * @Description:如何显示在星期一至五的名称
 * @author: xyc 
 * @date:   2017年2月10日 下午3:58:04   
 *
 */
public class DisplayWeekday {
    
   public static void main(String[] args) {
       
      String[] weekdays = new DateFormatSymbols().getWeekdays();
      for (int i = 2; i < (weekdays.length-1); i++) {
         String weekday = weekdays[i];
         System.out.println("weekday = " + weekday);
      }
      
   }
}
View Code

上面的代码示例将产生以下结果:

weekday = 星期一
weekday = 星期二
weekday = 星期三
weekday = 星期四
weekday = 星期五

  

如何添加时间(天,年,秒)到一个日期?

package com.xyc.formatInstance;
import java.util.*;

/**
 * 
 * @ClassName:  Main   
 * @Description:如何添加时间(天,年,秒)到一个日期?
 * @author: xyc 
 * @date:   2017年2月10日 下午3:59:39   
 *
 */
public class AddTime {
   public static void main(String[] args) throws Exception {
      
       Date d1 = new Date();
      Calendar cl = Calendar. getInstance();
      cl.setTime(d1);
      System.out.println("today is " + d1.toString());
      //一个月后
      cl. add(Calendar.MONTH, 1);
      System.out.println("date after a month will be " + cl.getTime().toString() );
      
      //70小时候
      cl. add(Calendar.HOUR, 70);
      System.out.println("date after 70 hrs will be " + cl.getTime().toString() );
      
      //三年后
      cl. add(Calendar.YEAR, 3);
      System.out.println("date after 3 years will be " + cl.getTime().toString() );
   }
}
View Code

上面的代码示例将产生以下结果:

today is Fri Feb 10 16:38:38 CST 2017
date after a month will be Fri Mar 10 16:38:38 CST 2017
date after 70 hrs will be Mon Mar 13 14:38:38 CST 2017
date after 3 years will be Fri Mar 13 14:38:38 CST 2020

  

如何以不同的格式显示日期?

package com.xyc.formatInstance;

import java.text.*;
import java.util.*;

/**
 * 
 * @ClassName:  Main   
 * @Description:如何以不同的格式显示日期?
 * @author: xyc 
 * @date:   2017年2月10日 下午4:24:12   
 *
 */

public class DiffFormat {
    public static void main(String[] args) {
        
        Date dt = new Date(1000000000000L);

        DateFormat[] dtformat = new DateFormat[6];
        dtformat[0] = DateFormat.getInstance();
        dtformat[1] = DateFormat.getDateInstance();
        dtformat[2] = DateFormat.getDateInstance(DateFormat.MEDIUM);
        dtformat[3] = DateFormat.getDateInstance(DateFormat.FULL);
        dtformat[4] = DateFormat.getDateInstance(DateFormat.LONG);
        dtformat[5] = DateFormat.getDateInstance(DateFormat.SHORT);

        for (DateFormat dateform : dtformat)
            System.out.println(dateform.format(dt));
    }
}
View Code

上面的代码示例将产生以下结果:

01-9-9 上午9:46
2001-9-9
2001-9-9
2001年9月9日 星期日
2001年9月9日
01-9-9

  

posted on 2017-02-10 16:27  天道|酬勤  阅读(254)  评论(0编辑  收藏  举报

导航