有些代码按了复制键没有效果,但是其实已经复制到剪贴板上面了,大家请注意哦!
我的文章有时会稍有修改,转载请注明出处哦!
原文地址:SimpleDateFormat 12小时制以及24小时制的写法
去代码
- package com.test;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- /**
- * SimpleDateFormat12小时和24小时制测试类
- * @author 水山清风
- *
- */
- public class SimpleDateFormatTest {
- /**
- * @param args
- */
- public static void main(String[] args) {
- String sTime = "2010-11-02";// 开始时间
- String eTime = "2010-11-10";// 结束时间
- sTime += " 00:00"; //2010-11-02 00:00
- eTime += " 23:59"; //2010-11-10 23:59
- Date now = new Date();
- SimpleDateFormat format12 = new SimpleDateFormat("yyyy-MM-dd hh:mm");
- SimpleDateFormat format24 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
- try {
- //12小时制
- //12小时制format sTime = 2010-11-02 12:00
- System.out.println("12小时制format sTime = " + format12.format(format12.parse(sTime)));
- //12小时制format eTime = 2010-11-10 11:59
- System.out.println("12小时制format eTime = " + format12.format(format24.parse(eTime)));
- //24小时制
- //24小时制format sTime = 2010-11-02 00:00
- System.out.println("24小时制format sTime = " + format24.format(format12.parse(sTime)));
- //24小时制format eTime = 2010-11-10 23:59
- System.out.println("24小时制format eTime = " + format24.format(format24.parse(eTime)));
- //在这种情况下format12.parse()和format24.parse()是一样的
- //12小时制
- //Tue Nov 02 14:00:00 GMT+08:00 2010
- System.out.println(format12.parse("2010-11-02 14:00"));
- //24小时制
- //Tue Nov 02 14:00:00 GMT+08:00 2010
- System.out.println(format24.parse("2010-11-02 14:00"));
- } catch (ParseException e) {
- e.printStackTrace();
- }
- }
- }