package org.fun.classdemo;

import java.util.Calendar;
import java.util.GregorianCalendar; public class DateTime {
private Calendar calendar = new GregorianCalendar(); // 实例化Calendar对象 public String getDate() {// 2014-07-30
StringBuffer buf = new StringBuffer();
buf.append(calendar.get(Calendar.YEAR)).append("-");
buf.append(this.addZero((calendar.get(Calendar.MONTH) + 1), 2)).append(
"-");
buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH), 2));
return buf.toString();
} public String getDateTime() {// 2014-07-30 10:19:34.123
StringBuffer buf = new StringBuffer();
buf.append(this.getDate()).append(" ");
buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY), 2)).append(
":");
buf.append(this.addZero(calendar.get(Calendar.MINUTE), 2)).append(":");
buf.append(this.addZero(calendar.get(Calendar.SECOND), 2)).append(".");
buf.append(this.addZero(calendar.get(Calendar.MILLISECOND), 3));
return buf.toString();
} public String getDateComplete() {// 2014年07月30日
StringBuffer buf = new StringBuffer();
buf.append(calendar.get(Calendar.YEAR)).append("年");
buf.append(this.addZero((calendar.get(Calendar.MONTH) + 1), 2)).append(
"月");
buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH), 2))
.append("日");
return buf.toString();
} public String getDateTimeComplete() {// 2014年07月30日10时19分34秒123毫秒
StringBuffer buf = new StringBuffer();
buf.append(this.getDateComplete());
buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY), 2)).append(
"时");
buf.append(this.addZero(calendar.get(Calendar.MINUTE), 2)).append("分");
buf.append(this.addZero(calendar.get(Calendar.SECOND), 2)).append("秒");
buf.append(this.addZero(calendar.get(Calendar.MILLISECOND), 3)).append(
"毫秒");
return buf.toString();
} private String addZero(int temp, int len) {
StringBuffer str = new StringBuffer();
str.append(temp);// 增加数字
while (str.length() < len) {
str.insert(0, 0); // 在第一个位置加上字母0
}
return str.toString();
} public static void main(String args[]) {
System.out.println(new DateTime().getDate());
System.out.println(new DateTime().getDateTime());
System.out.println(new DateTime().getDateComplete());
System.out.println(new DateTime().getDateTimeComplete());
}
}

JavaLearning:日期操作类的更多相关文章

  1. 使用日期操作类(Calendar)获得几秒、几分钟、几小时之前的时间

    public String dealDate(String case_time){ // 日期操作类 Calendar calendar = Calendar.getInstance(); // 当前 ...

  2. JAVA笔记10__Math类、Random类、Arrays类/日期操作类/对象比较器/对象的克隆/二叉树

    /** * Math类.Random类.Arrays类:具体查JAVA手册...... */ public class Main { public static void main(String[] ...

  3. 菜鸡的Java笔记 日期操作类

    日期操作类        Date 类与 long 数据类型的转换        SimpleDateFormat 类的使用        Calendar 类的使用                如 ...

  4. 日期操作类--Date类

    Date-API ava.util包提供了Date类来封装当前的日期和时间.Date类提供两个构造函数来实例化Date对象.第一个构造函数使用当前日期和时间来初始化对象. Date( ) 第二个构造函 ...

  5. 日期操作类--SimpleDateFormat类

    使用SimpleDateFormat格式化日期 SimpleDateFormat是一个以语言环境敏感的方式来格式化和分析日期的类.SimpleDateFormat允许你选择任何用户自定义日期时间格式来 ...

  6. 日期操作类--GregorianCalendar类

    GregorianCalendar--API JavaTM Platform Standard Ed. 6 GregorianCalendar类 Calendar类实现了公历日历,GregorianC ...

  7. 日期操作类--DateFormat类

    简单的DateFormat格式化编码 时间模式字符串用来指定时间格式.在此模式中,所有的ASCII字母被保留为模式字母,定义如下: 字母 描述 示例 G 纪元标记 AD y 四位年份 2001 M 月 ...

  8. 日期操作类--Calendar类

    Calendar-API Calendar类 通过Date和DateFormat能够格式化并创建一个日期对象了,但是我们如何才能设置和获取日期数据的特定部分呢,比如说小时,日,或者分钟? 我们又如何在 ...

  9. Java面向对象_常用类库api——日期操作类

    Data类 类Data表示特定的瞬间,精确到毫秒,也就是程序运行时的当前时间 Data data=new Data();//实例化Data对象,表示当前时间 Calendar类 日历类,使用此类可以将 ...

随机推荐

  1. springboot shiro配置

    导入相关包(这里配合使用Ehcache缓存) <dependency> <groupId>org.apache.shiro</groupId> <artifa ...

  2. POJ 3668 枚举?

    枚举两点,算一下斜率 sort一遍 判个重 输出解 25行 搞定- //By SiriusRen #include <cmath> #include <cstdio> #inc ...

  3. jqueryValidator自定义校验规则的一种方式(覆盖源码)

    1.自定义js文件:jqValid-extend.js 内容: function setDefaultValidate(){ $.extend(true, $.validator, { // 方法 m ...

  4. 为一个支持GPRS的硬件设备搭建一台高并发服务器用什么开发比较容易?

    高并发服务器开发,硬件socket发送数据至服务器,服务器对数据进行判断,需要实现心跳以保持长连接. 同时还要接收另外一台服务器的消支付成功消息,接收到消息后控制硬件执行操作. 查了一些资料,java ...

  5. 杀死超过5min闲置的终端

    #!/bin/bash #杀死超过5min闲置的终端 while [ 1 -lt 2 ] do sleep 30 for i in `w -sh | grep ":" | awk ...

  6. HN0I2000最优乘车 (最短路变形)

    HN0I2000最优乘车 (最短路变形) 版权声明:本篇随笔版权归作者YJSheep(www.cnblogs.com/yangyaojia)所有,转载请保留原地址! [试题]为了简化城市公共汽车收费系 ...

  7. 洛谷 P1754 球迷购票问题

    P1754 球迷购票问题 题目背景 盛况空前的足球赛即将举行.球赛门票售票处排起了球迷购票长龙. 按售票处规定,每位购票者限购一张门票,且每张票售价为50元.在排成长龙的球迷中有N个人手持面值50元的 ...

  8. 浴谷 P1768 天路

    P1768 天路 题目描述 “那是一条神奇的天路诶~,把第一个神犇送上天堂~”,XDM先生唱着这首“亲切”的歌曲,一道猥琐题目的灵感在脑中出现了. 和C_SUNSHINE大神商量后,这道猥琐的题目终于 ...

  9. Java Exception和Error的差别

    Java中异常的抽象类是Throwable,在此基础上.派生出两大类:Error和Exception. Error是程序中的严重错误,不应该用try-catch包括.Javadoc的说明例如以下: A ...

  10. 使用maven运行单元測试总结

    maven本身没有单元測试框架,可是maven的default生命周期的test阶段绑定了maven-surefire-plugin插件,该插件能够调用Junit3.Junit4.TestNG等Jav ...