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 ...
随机推荐
- nodejs表单验证
//创建express连接 var exp = require('xepress'), http = require('http'); //初始化exprerss模块 var app = exp(); ...
- Spring AOP:面向切面编程,AspectJ,是基于spring 的xml文件的方法
导包等不在赘述: 建立一个接口:ArithmeticCalculator,没有实例化的方法: package com.atguigu.spring.aop.impl.panpan; public in ...
- 2016年11月6日 星期日 --出埃及记 Exodus 19:22
2016年11月6日 星期日 --出埃及记 Exodus 19:22 Even the priests, who approach the LORD, must consecrate themselv ...
- 2016年10月12日 星期三 --出埃及记 Exodus 18:23
2016年10月12日 星期三 --出埃及记 Exodus 18:23 If you do this and God so commands, you will be able to stand th ...
- vim基本使用
i 进入插入状态 esc 退出插入状态 x 删除一个字符 dd 删除一行,并拷贝 yy 拷贝 p 粘贴 u 撤销 ctrl+r 重做 :w 保存 :q 退出 :q! → 退出不保存
- 窗口移动--基类(BaseForm)
#region 窗口移动 private bool _isLeftButtonDown = false; public const int HTCAPTION = 0x0002; protected ...
- JAVA 语言基础——运算符
1.赋值运算符 赋值运算符"=",是一个能对两个操作数进行处理的二元运算符. 比如: int a = 12; //声明int型变量a int b= 34; //声明int型变量b ...
- Walkthrough: Arranging Controls on Windows Forms Using Snaplines
https://msdn.microsoft.com/en-us/library/t5b5kc41(v=vs.110).aspx Spacing and Aligning Controls Using ...
- 命令行运行R语言脚本(代码)
1 Windows: 键入 cd C:\Program Files\R\R-3.2.0\bin 工作目录切换到R的核心程序目录 键入 R BATCH F:\Test.R 或 Rscript F:\ ...
- 大神写的一个纯CSS圆角框,膜拜!(支持IE9一下的低版本)
留着提醒自己,底层才是最重要的,不要一直傻瓜的编程下去! <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...