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. 6. Intellij Idea 2017创建web项目及tomcat部署实战

    转自:https://www.cnblogs.com/shindo/p/7272646.html 相关软件:Intellij Idea2017.jdk16.tomcat7 Intellij Idea直 ...

  2. 基于JavaSwing的例子-非连接数据库

    项目结构: Constant.java package com.mstf.test; import java.io.Serializable; public class Constant implem ...

  3. <Sicily> 生成字符串

    一.题目描述 假设一个字符串只由字符'0','1','?'组成,其中字符'?'表示该字符可由字符'0'或'1'替代. 现有一些字符串,根据这些字符串生成所有可生成的字符串. 如:{10,?1,0? } ...

  4. UVa 208 Firetruck【回溯】

    题意:给出一个n个节点的无向图,以及某个节点k,按照字典序从小到大输出从节点1到节点k的所有路径 看的题解 http://blog.csdn.net/hcbbt/article/details/975 ...

  5. GoldenGate 应用系统升级

    (仅复制DML时)源端和目标端数据库增减复制表 增加复制表 在GoldenGate的进程参数中,如果通过*来匹配所有表,因此只要符合*所匹配的条件,那么只要在源端建立了表之后GoldenGate就能自 ...

  6. DNS Prefetching

    For Developers‎ > ‎Design Documents‎ > ‎ DNS Prefetching 目录 1 Problem 2 Solution 3 Architectur ...

  7. Mac安装composer安装Yii2项目

    [注释:]本人原创,如需转载请注明来源链接! 通过安装Composer方式安装Yii 如果还没有安装 Composer,你可以按 getcomposer.org 中的方法安装. 在 Linux 和 M ...

  8. SQL Server存储ntext截断问题

    SQL Server存储ntext截断问题   最近遇到一个问题:将大文本存储到数据库的时候,查询出来的文本却被截断了. 最后百度发现,作者提出 sql server management studi ...

  9. MFC打包生成可安装文件

    一.问题来源: VS环境:编写的MFC程序即使静态联编之后,在客户机器上还是打不开.可能的原因是你开发的时候调用了第三方ActiveX控件.这时候就需要把自己的release版的发布软件和*.ocx控 ...

  10. Python安装selenium启动浏览器

    1:在Python运行火狐或谷歌的浏览器是需要下载相对应的驱动 例如:你想在Python中使用代码命令打开firefox的网页 如果没有安装驱动,直接运行的话会出下面的错误 所以我们要安装相对应的浏览 ...