Java Date与SimpleDateFormat
最近在弄一些涉及到时间处理的项目。本来自己写了一个时间转换函数,虽然能用但是过于麻烦而且不够规范,于是学习了下java自带的时间处理的类。
public class Timechg {
public static int ymd[][][]= new int[110][13][33];
public static int day[][][] = new int[25][61][61];
public static int _ymd[][] = new int[110*13*33+1][3];
public static int _day[][] = new int[25*61*61+1][3];
public static int save[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
public final static int ONEDAYS = 24*60*60;
public static void timeinit()
{
int y=2000,m=1,d=1;
int cnt=0;
ymd[y-2000][m][d] = cnt;
_ymd[cnt][0]=y;_ymd[cnt][1]=m; _ymd[cnt][2]=d;
while(y<2100)
{
if( (y%4==0&&y%100!=0)||(y%400==0) ) save[2]=29;
else save[2]=28;
d++;
cnt++;
if(d>save[m])
{
m++;
d=1;
if(m>12)
{
y++;
m=1;
}
}
ymd[y-2000][m][d] = cnt;
_ymd[cnt][0]=y;_ymd[cnt][1]=m; _ymd[cnt][2]=d;
}
int h=0,s=0;
m=0;// 时,分,秒
cnt=0;
day[h][m][s]=cnt;
_day[cnt][0]=h; _day[cnt][1]=m; _day[cnt][2]=s;
while(true)
{
cnt++;
s++;
if(s==60)
{
s=0;
m++;
if(m==60)
{
m=0;
h++;
if(h==24)
{
break;
}
}
}
day[h][m][s] = cnt;
_day[cnt][0]=h; _day[cnt][1]=m; _day[cnt][2]=s;
}
}
/**
* time 的格式为yyyyMMddHHmmSS
* @param time
* @return
*/
public static int strtoint(String time)
{
int y,M,d,H,m,S;
y = Integer.parseInt( time.substring(0, 4) );
M = Integer.parseInt( time.substring(4, 6) );
d = Integer.parseInt( time.substring(6, 8) );
H = Integer.parseInt( time.substring(8, 10) );
m = Integer.parseInt( time.substring(10, 12) );
S = Integer.parseInt( time.substring(12, 14) );
return ymd[ y-2000<0?0:y-2000 ][M][d]*ONEDAYS + day[H][m][S];
}
public static String inttostr(int time)
{
StringBuffer timestr=new StringBuffer("");
int intymd,intday;
intymd = time/ONEDAYS;
intday = time%ONEDAYS;
timestr.append(String.format("%04d",_ymd[intymd][0]));
timestr.append(String.format("%02d",_ymd[intymd][1]));
timestr.append(String.format("%02d",_ymd[intymd][2]));
timestr.append(String.format("%02d",_day[intday][0]));
timestr.append(String.format("%02d",_day[intday][1]));
timestr.append(String.format("%02d",_day[intday][2]));
return timestr.toString();
}
}
垃圾代码
例1 格式化输出当前系统时间
Date date=new Date();//获取当前时间
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(df.format(date));
例2 将字符串格式时间转化为时间戳
SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
Date date=null;
try {
date = simpleDateFormat.parse("2010-06-25-00-00-00");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long time = date.getTime();//从1970年1月1日开始精确的毫秒,若要得到秒为单位,需要/1000
System.out.println(time);
例3 得到Date对象内的年月日等
Date date=new Date();
System.out.println(date.toString());
System.out.println(date.getYear()+1900);
System.out.println(date.getMonth()+1);
System.out.println(date.getDate());//注意date.getday()是获得星期几,0-6分别表示从星期日,星期一,。。。,星期六
Java Date与SimpleDateFormat的更多相关文章
- Java基础(37):Java中日期的显示与格式定值----Date与SimpleDateFormat的试用
使用 Date 和 SimpleDateFormat 类表示时间 在程序开发中,经常需要处理日期和时间的相关数据,此时我们可以使用 java.util 包中的 Date 类.这个类最主要的作用就是获取 ...
- Java 中Calendar、Date、SimpleDateFormat学习总结
在之前的项目中,经常会遇到Calendar,Date的一些操作时间的类,并且总会遇到时间日期之间的格式转化问题,虽然做完了但是总是忘记,记不清楚,每次还都要查找资料.今天总结一下,加深印象. Cale ...
- Java之StringBuffer,StringBuilder,Math,Date,SimpleDateFormat,UUID,File
java.lang 类 StringBuffer java.lang.Object java.lang.StringBuffer 所有已实现的接口: Serializable, Appendable, ...
- Java学习--使用 Date 和 SimpleDateFormat 类表示时间
使用 Date 和 SimpleDateFormat 类表示时间 在程序开发中,经常需要处理日期和时间的相关数据,此时我们可以使用 java.util 包中的 Date 类.这个类最主要的作用就是获取 ...
- Java—包装类、Date和SimpleDateFormat、Calendar类
包装类 基本数据类型不能调用方法,功能简单,为了让基本数据类型也具备对象的特性,Java为每个基本数据类型提供了一个包装类,这样就可以像操作对象那样来操作基本数据类型. 基本类型和包装类之间的对应关系 ...
- java日期处理SimpleDateFormat等
1.mysql数据库中有这样一个表: mysql> select * from test_table;+----------+---------------------+| username | ...
- 使用 Date 和 SimpleDateFormat 类表示时间、Calendar类和Math类
一. Date 和 SimpleDateFormat类表示时间 在程序开发中,经常需要处理日期和时间的相关数据,此时我们可以使用 java.util 包中的 Date 类.这个类最主要的作用就是获取当 ...
- Java Date,long,String 日期转换
1.java.util.Date类型转换成long类型java.util.Date dt = new Date();System.out.println(dt.toString()); //java. ...
- 使用 Date 和 SimpleDateFormat 类表示时间
在程序开发中,经常需要处理日期和时间的相关数据,此时我们可以使用 java.util 包中的Date类.这个类最主要的作用就是获取当前时间,我们来看下Date的类的使用: Date d=new Dat ...
随机推荐
- javaWEB国际化(jsp中使用)
在jsp页面中使用国际化方法,首先将jstl开源架包:jstl.jar,standard.jar导进去 并在src目录下建立以test开头,.properties结尾的文件:test_en_US.pr ...
- 2015-09-17 001 日志与对话框公用类_public
using System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using S ...
- Check the difficulty of problems
Check the difficulty of problems Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5830 Acc ...
- Linux 文件操作总结
http://blog.163.com/he_junwei/blog/static/19793764620152592737741/ ioctl?? lseek?? 文件是linux中的一个重要概念. ...
- 1. WP8.1学习笔记
数据绑定 含义:将对象绑定到控件上 2.基本名词 控件:绑定目标 对象:绑定源(数据源) 控件与对象属性的联系:路径 如何绑定 创建对象,设置控件 在控件需要数据绑定的地方使用拓展语法 <But ...
- flash压力测试
涉及目录: vendor/mediatek/proprietary/bootable/bootloader/preloader/platform/mt6735/src/drivers/inc/dram ...
- 精通D3.js学习笔记(1)基础的函数
买了本吕大师的d3可视化.最近来学习一下,做个笔记. 1.选择元素 select(第一元素) 和selectAll(全部的元素) 类似css的选择器.也可以是dom选中的. var i ...
- 2016年7月2日 星期六 --出埃及记 Exodus 14:29
2016年7月2日 星期六 --出埃及记 Exodus 14:29 But the Israelites went through the sea on dry ground, with a wall ...
- SQL Server常用函数
---------数学函数 ) --开平方 ) --区绝对值 ) --保留小数点后1位 四舍五入保留 select COUNT(*)/5.0 from Car select CEILING(COUNT ...
- Linux常见问题的处理方法(长期更新)
一.使用sudo命令时xxx is not in the sudoers file. This incident will be reported. 1.su -,输入root的密码完成身份切换. 2 ...