JavaLearning:日期操作类
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:日期操作类的更多相关文章
- 使用日期操作类(Calendar)获得几秒、几分钟、几小时之前的时间
public String dealDate(String case_time){ // 日期操作类 Calendar calendar = Calendar.getInstance(); // 当前 ...
- JAVA笔记10__Math类、Random类、Arrays类/日期操作类/对象比较器/对象的克隆/二叉树
/** * Math类.Random类.Arrays类:具体查JAVA手册...... */ public class Main { public static void main(String[] ...
- 菜鸡的Java笔记 日期操作类
日期操作类 Date 类与 long 数据类型的转换 SimpleDateFormat 类的使用 Calendar 类的使用 如 ...
- 日期操作类--Date类
Date-API ava.util包提供了Date类来封装当前的日期和时间.Date类提供两个构造函数来实例化Date对象.第一个构造函数使用当前日期和时间来初始化对象. Date( ) 第二个构造函 ...
- 日期操作类--SimpleDateFormat类
使用SimpleDateFormat格式化日期 SimpleDateFormat是一个以语言环境敏感的方式来格式化和分析日期的类.SimpleDateFormat允许你选择任何用户自定义日期时间格式来 ...
- 日期操作类--GregorianCalendar类
GregorianCalendar--API JavaTM Platform Standard Ed. 6 GregorianCalendar类 Calendar类实现了公历日历,GregorianC ...
- 日期操作类--DateFormat类
简单的DateFormat格式化编码 时间模式字符串用来指定时间格式.在此模式中,所有的ASCII字母被保留为模式字母,定义如下: 字母 描述 示例 G 纪元标记 AD y 四位年份 2001 M 月 ...
- 日期操作类--Calendar类
Calendar-API Calendar类 通过Date和DateFormat能够格式化并创建一个日期对象了,但是我们如何才能设置和获取日期数据的特定部分呢,比如说小时,日,或者分钟? 我们又如何在 ...
- Java面向对象_常用类库api——日期操作类
Data类 类Data表示特定的瞬间,精确到毫秒,也就是程序运行时的当前时间 Data data=new Data();//实例化Data对象,表示当前时间 Calendar类 日历类,使用此类可以将 ...
随机推荐
- 9.优先队列,priority_queue
#include <iostream> #include <queue> #include <deque> #include <list> using ...
- <Sicily>Fibonacci 2
一.题目描述 In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn-1 + Fn-2 for n ≥ 2. For exampl ...
- codeforces 527 C Glass Carving
Glass Carving time limit per test 2 seconds Leonid wants to become a glass carver (the person who cr ...
- SCO Openserver、SCO Unix、SCO UnixWare、Solaris几者到底是什么关系,有什么相同或不同?
Unix操作系统的历史漫长而曲折,它的第一个版本是1969年由Ken Thompson在AT&T贝尔实验室实现的,运行在一台DEC PDP-7计算机上.这个系统非常粗糙,与现代Unix相差很远 ...
- NodeJS学习笔记 进阶 (1)Nodejs进阶:服务端字符编解码&乱码处理(ok)
个人总结:这篇文章主要讲解了Nodejs处理服务器乱码及编码的知识,读完这篇文章需要10分钟. 摘选自网络 写在前面 在web服务端开发中,字符的编解码几乎每天都要打交道.编解码一旦处理不当,就会出现 ...
- ES6学习笔记(十四)Generator函数
清明时节雨纷纷,路上行人欲断魂. 借问酒家何处有,牧童遥指杏花村. 二零一九年农历三月初一,清明节. 1.简介 1.1.基本概念 Generator 函数也是 ES6 提供的一种异步编程解决方案,据说 ...
- 第四讲 Yang-Mills方程与Maxwell方程
一.变分原理 变分原理始于17世纪的速降问题,也就是连接两点的曲线在有重力的情况下,让初速度为0的一小球最快地通过? 这个问题由伯努力给出解答,他的方法非常巧妙,而最后开创了一个学科——变分学.他假设 ...
- Linux学习之socket编程(二)
Linux学习之socket编程(二) 1.C/S模型——UDP UDP处理模型 由于UDP不需要维护连接,程序逻辑简单了很多,但是UDP协议是不可靠的,实际上有很多保证通讯可靠性的机制需要在应用层实 ...
- 137 - ZOJ Monthly, November 2014 - J Poker Face
Poker Face Time Limit: 2 Seconds Memory Limit: 65536 KB As is known to all, coders are lack of ...
- 洛谷 P1952 火星上的加法运算_NOI导刊2009提高(3)
P1952 火星上的加法运算_NOI导刊2009提高(3) 题目描述 最近欢欢看到一本有关火星的书籍,其中她被一个加法运算所困惑,由于她的运算水平有限.她想向你求助,作为一位优秀的程序员,你当然不会拒 ...