吴裕雄--天生自然java开发常用类库学习笔记:取得当前日期
import java.util.* ; // 导入需要的工具包
class DateTime{ // 以后直接通过此类就可以取得日期时间
private Calendar calendar = null ; // 声明一个Calendar对象,取得时间
public DateTime(){ // 构造方法中直接实例化对象
this.calendar = new GregorianCalendar() ;
}
public String getDate(){ // 得到的是一个日期:格式为:yyyy-MM-dd HH:mm:ss.SSS
// 考虑到程序要频繁修改字符串,所以使用StringBuffer提升性能
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(" ") ; // 取得日
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(){ // 得到的是一个日期:格式为:yyyy年MM月dd日 HH时mm分ss秒SSS毫秒
// 考虑到程序要频繁修改字符串,所以使用StringBuffer提升性能
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("日") ; // 取得日
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() ;
}
public String getTimeStamp(){ // 得到的是一个时间戳
// 考虑到程序要频繁修改字符串,所以使用StringBuffer提升性能
StringBuffer buf = new StringBuffer() ;
buf.append(calendar.get(Calendar.YEAR)) ; // 增加年
buf.append(this.addZero(calendar.get(Calendar.MONTH)+1,2)) ; // 增加月
buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH),2)) ; // 取得日
buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY),2)) ; // 取得时
buf.append(this.addZero(calendar.get(Calendar.MINUTE),2)) ; // 取得分
buf.append(this.addZero(calendar.get(Calendar.SECOND),2)); // 取得秒
buf.append(this.addZero(calendar.get(Calendar.MILLISECOND),3)) ; // 取得毫秒
return buf.toString() ;
}
// 考虑到日期中存在前导0,所以在此处加上补零的方法
private String addZero(int num,int len){
StringBuffer s = new StringBuffer() ;
s.append(num) ;
while(s.length()<len){ // 如果长度不足,则继续补0
s.insert(0,"0") ; // 在第一个位置处补0
}
return s.toString() ;
}
};
public class DateDemo06{
public static void main(String args[]){
DateTime dt = new DateTime() ;
System.out.println("系统日期:"+dt.getDate()) ;
System.out.println("中文日期:"+dt.getDateComplete()) ;
System.out.println("时间戳:"+dt.getTimeStamp()) ;
}
};
import java.util.* ; // 导入需要的工具包
import java.text.* ; // 导入SimpleDateFormat所在的包
class DateTime{ // 以后直接通过此类就可以取得日期时间
private SimpleDateFormat sdf = null ; // 声明SimpleDateFormat对象
public String getDate(){ // 得到的是一个日期:格式为:yyyy-MM-dd HH:mm:ss.SSS
this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") ;
return this.sdf.format(new Date()) ;// 将当前日期进行格式化操作
}
public String getDateComplete(){ // 得到的是一个日期:格式为:yyyy年MM月dd日 HH时mm分ss秒SSS毫秒
this.sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒SSS毫秒") ;
return this.sdf.format(new Date()) ;// 将当前日期进行格式化操作
}
public String getTimeStamp(){ // 得到的是一个时间戳
this.sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS") ;
return this.sdf.format(new Date()) ;// 将当前日期进行格式化操作
}
};
public class DateDemo07{
public static void main(String args[]){
DateTime dt = new DateTime() ;
System.out.println("系统日期:"+dt.getDate()) ;
System.out.println("中文日期:"+dt.getDateComplete()) ;
System.out.println("时间戳:"+dt.getTimeStamp()) ;
}
};
吴裕雄--天生自然java开发常用类库学习笔记:取得当前日期的更多相关文章
- 吴裕雄--天生自然java开发常用类库学习笔记:定时调度
// 完成具体的任务操作 import java.util.TimerTask ; import java.util.Date ; import java.text.SimpleDateFormat ...
- 吴裕雄--天生自然java开发常用类库学习笔记:正则表达式
public class RegexDemo01{ public static void main(String args[]){ String str = "1234567890" ...
- 吴裕雄--天生自然java开发常用类库学习笔记:观察者设计模式
import java.util.* ; class House extends Observable{ // 表示房子可以被观察 private float price ;// 价钱 public ...
- 吴裕雄--天生自然java开发常用类库学习笔记:比较器
class Student implements Comparable<Student> { // 指定类型为Student private String name ; private i ...
- 吴裕雄--天生自然java开发常用类库学习笔记:Arrays
import java.util.* ; public class ArraysDemo{ public static void main(String arg[]){ int temp[] = {3 ...
- 吴裕雄--天生自然java开发常用类库学习笔记:大数操作
import java.math.* ; class MyMath{ public static double add(double d1,double d2){ // 进行加法计算 BigDecim ...
- 吴裕雄--天生自然java开发常用类库学习笔记:NumberFormat
import java.text.* ; public class NumberFormatDemo01{ public static void main(String args[]){ Number ...
- 吴裕雄--天生自然java开发常用类库学习笔记:Math与Random类
public class MathDemo01{ public static void main(String args[]){ // Math类中的方法都是静态方法,直接使用“类.方法名称()”的形 ...
- 吴裕雄--天生自然java开发常用类库学习笔记:日期操作类DataFormat、SimpleDataFormat
import java.text.DateFormat ; import java.util.Date ; public class DateDemo03{ public static void ma ...
随机推荐
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 显示代码:按键提示
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 都客仿站高手已注册旗舰版V3.1
链接:https://pan.baidu.com/s/1R5ldFDjekuXmEp42-8SQSQ 提取码:gkm9
- Educational Codeforces Round 72 (Rated for Div. 2)D(DFS,思维)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int n,m,k=1;int c[5007] ...
- 好用的log打印类
package com.huawei.network.ott.weixin.util; import android.util.Log; public final class DebugLog { / ...
- LTE 网元功能
E-NodeB : 无线资源管理,无线承载控制.无线接入控制.连接移动性控制.UE的上下行动态资源分配 IP头压缩及用户数据流加密 UE连接期间选择MME 路由用户面数据至S-GW 寻呼消息的组织和发 ...
- 设计模式课程 设计模式精讲 9-2 原型模式coding
1 课堂演练 1.1 super.toString 作用 1.2 为什么要使用克隆方法呢 2 代码解析 2.1 代码解析1(使用原型模式之前) 2.2 代码解析2(使用原型模式默认方式(浅克隆)) 2 ...
- 普通用户切换不到root用户--权限更改
https://blog.csdn.net/lianjoke0/article/details/82598149 [root@java133 ~]# ll /etc/passwd -rw-r--r-- ...
- 微信小程序中,如何实现显示,隐藏密码的功能
最近在搞小程序的开发,遇到隐藏,显示密码的功能的时候,电脑上调试没问题,但是手机上面点击却没有效果,必须要跳转到其他页面再跳回来,才能正常显示. 一时间搞得我很头疼,查找资料后,终于知道了是什么原因. ...
- android 动态壁纸开发
转:http://www.eoeandroid.com/thread-100389-1-1.html android 动态壁纸开发参考:http://www.ophonesdn.com/article ...
- windows下用libevent 开发一个echo服务
#include <stdio.h> #include <string.h> #include <errno.h> #include <iostream> ...