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 ...
随机推荐
- Java学习之路(一)
小知识: 1:1973年 贝尔实验室 => c语言 2:sun 的意思是:斯坦福大学校园网 3::1994 oak 被命名为Java JDK(Java开发工具包) 1:下载JDK 2:安装JD ...
- 使用oracle写if判断
DECLARE L_X INT; BEGIN SELECT COUNT(*) INTO L_X FROM SYSTEMROLEFUNCTION WHERE ROLEID = '1'; IF L_X & ...
- STM32/GD32上内存堆栈溢出探测研究
无数次遭受堆栈溢出折磨,随着系统变得复杂,故障点越来越难以查找!主要溢出情况如下:1,一般RAM最后两块空间是堆Heap和栈Stack,堆从下往上用,栈从上往下用,任意一个用完,都会进入对方的空间2, ...
- Jquery中的filter()详细说明和transition的用法
filter() 方法将匹配元素集合缩减为匹配指定选择器的元素. 详细说明 如果给定表示 DOM 元素集合的 jQuery 对象,.filter() 方法会用匹配元素的子集构造一个新的 jQuery ...
- PHP处理数据--excel与scv与json
今天要处理两个excel.两个循环嵌套验证重复性.所以写了几个函数来处理20亿次的数据量. 一.把excel读出来,保存为json.利用phpexcel插件: <?php header(&quo ...
- MFC设置对话框大小
设置对话框大小不可改变 1.在类的头文件中加入函数申明 (.h文件) afx_msg void OnGetMinMaxInfo(MINMAXINFO *lpMMI); 2.在消息映射中添加 (.cpp ...
- .Net自帶Ajax和GridView
如圖所示,在新建web窗體后的工具欄中有一個 AJAX擴展 ScriptManager 在整個網頁中有且只有一個,使用母版頁和用戶控件中尤為注意, 例如在嵌套母版頁和用戶控件時只在最外層加上Scrip ...
- html弹出div弹窗
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- 创立数据库表 examstudent
package com.hanqi.test; import java.sql.*; import java.util.*; public class LianXi { public static v ...
- include_path详细解析
include_path详细解析 原文地址:http://www.laruence.com/2010/05/04/1450.html 1.php默认的包含路径为 .;C:\php\pear 即 ...