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类 日历类,使用此类可以将 ...
随机推荐
- Cms WebSite 编译非常慢
第一次编译非常慢 如果遇到错误,中途中断的话. 下一次编译的时候,上一次已经编译过的文件,会非常快的略过.很快就会到上一次遇到错误的地方,继续往下进行编译.
- m_Orchestrate learning system---二十五、复制类的时候最容易出现的错误是什么
m_Orchestrate learning system---二十五.复制类的时候最容易出现的错误是什么 一.总结 一句话总结:命名空间错误导致Analyze类虽然继承了Base类,但是没有执行里面 ...
- java动态导出PDF(利用itext)
项目基于ssm框架,使用itext动态导出pdf文件: 1.引入两个jar包:itextpdf-5.5.5.jar.itext-asian-5.2.0.jar 说明: 1.itextpdf-5.5.5 ...
- c# 结构 struct
结构是使用 struct 关键字定义的,与类相似,都表示可以包含数据成员和函数成员的数据结构. 一般情况下,我们很少使用结构,而且很多人也并不建议使用结构,但作为.NET Framework 一般型別 ...
- logsource and ALO
1.首先配置sourcedb上的nfs服务,oggstd上挂载sourcedb的online redo和archive log的目录 oggsource上配置: vi /etc/export ...
- NOIP2017普及组题
提高组死亡选手来AK普及(耗时两天). T1 #include<bits/stdc++.h> using namespace std; int A,B,C; int main() { ci ...
- ArchLinux 设置时间同步和硬件时间同步错误 No usable clock interface found
笔记本不知道怎么了,总是时间对不上 硬件时间也设置不了,只能时间同步了 手动时间同步 ntpdate即可,ntp服务器在这里用这两个就好了 cn.ntp.org.cn 或 edu.ntp.org.cn ...
- python描述符和属性查找
python描述符 定义 一般说来,描述符是一种访问对象属性时候的绑定行为,如果这个对象属性定义了__get__(),__set__(), and __delete__()一种或者几种,那么就称之为描 ...
- 通过JMeter来测试Quick Easy FTP Server的上传与下载性能
FTP性能测试 1.1背景说明 本测试选用的是一个小型的FTP服务器软件:Quick Easy FTP Server.Quick Easy FTP Server是一个全中文的FTP服务器软件,反应迅速 ...
- pip命令使用方法 与 错误处理
这里把学习到的pip命令写一个汇总,方便想不起来时使用 通过cmd输入pip可以显示提示信息,中文翻译如下: 1)显示某个包的信息 pip show selenium #显示selenium模块的信息 ...