最近在弄一些涉及到时间处理的项目。本来自己写了一个时间转换函数,虽然能用但是过于麻烦而且不够规范,于是学习了下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的更多相关文章

  1. Java基础(37):Java中日期的显示与格式定值----Date与SimpleDateFormat的试用

    使用 Date 和 SimpleDateFormat 类表示时间 在程序开发中,经常需要处理日期和时间的相关数据,此时我们可以使用 java.util 包中的 Date 类.这个类最主要的作用就是获取 ...

  2. Java 中Calendar、Date、SimpleDateFormat学习总结

    在之前的项目中,经常会遇到Calendar,Date的一些操作时间的类,并且总会遇到时间日期之间的格式转化问题,虽然做完了但是总是忘记,记不清楚,每次还都要查找资料.今天总结一下,加深印象. Cale ...

  3. Java之StringBuffer,StringBuilder,Math,Date,SimpleDateFormat,UUID,File

    java.lang 类 StringBuffer java.lang.Object java.lang.StringBuffer 所有已实现的接口: Serializable, Appendable, ...

  4. Java学习--使用 Date 和 SimpleDateFormat 类表示时间

    使用 Date 和 SimpleDateFormat 类表示时间 在程序开发中,经常需要处理日期和时间的相关数据,此时我们可以使用 java.util 包中的 Date 类.这个类最主要的作用就是获取 ...

  5. Java—包装类、Date和SimpleDateFormat、Calendar类

    包装类 基本数据类型不能调用方法,功能简单,为了让基本数据类型也具备对象的特性,Java为每个基本数据类型提供了一个包装类,这样就可以像操作对象那样来操作基本数据类型. 基本类型和包装类之间的对应关系 ...

  6. java日期处理SimpleDateFormat等

    1.mysql数据库中有这样一个表: mysql> select * from test_table;+----------+---------------------+| username | ...

  7. 使用 Date 和 SimpleDateFormat 类表示时间、Calendar类和Math类

    一. Date 和 SimpleDateFormat类表示时间 在程序开发中,经常需要处理日期和时间的相关数据,此时我们可以使用 java.util 包中的 Date 类.这个类最主要的作用就是获取当 ...

  8. Java Date,long,String 日期转换

    1.java.util.Date类型转换成long类型java.util.Date dt = new Date();System.out.println(dt.toString()); //java. ...

  9. 使用 Date 和 SimpleDateFormat 类表示时间

    在程序开发中,经常需要处理日期和时间的相关数据,此时我们可以使用 java.util 包中的Date类.这个类最主要的作用就是获取当前时间,我们来看下Date的类的使用: Date d=new Dat ...

随机推荐

  1. redis初试牛刀

    先来无事就学学redis.可是并没有想的那么美好.首先要解释一下,redis主流是安装在lunx系统中的,甚至官网直接没有给出windows版本.要下载windows只能去所谓的githup.好吧我在 ...

  2. c# UDP通信 列子

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. asp.net 关闭子窗体 刷新主窗体

    主窗体************************************************************************************ //原窗口保留,以对话框 ...

  4. awk用法小结(作者总结)

    http://www.chinaunix.net/old_jh/24/691456.html http://wenku.baidu.com/view/ebac4fc658f5f61fb736664d. ...

  5. fffffffffff

    create proc partPage114 @n int,--每页数量 @page int, --页码从0开始 @Mainkey int as declare @sql nvarchar(1000 ...

  6. anroid开发者专用vpn

    http://android.vpn.ac.cn/

  7. 《JAVA与模式》之策略模式

    <JAVA与模式>之策略模式 在阎宏博士的<JAVA与模式>一书中开头是这样描述策略(Strategy)模式的: 策略模式属于对象的行为模式.其用意是针对一组算法,将每一个算法 ...

  8. 修复跨站攻击 php

    在公用文件中加入如下代码 <?php /*云体检通用漏洞防护补丁v1.1 更新时间:2013-05-25 功能说明:防护XSS,SQL,代码执行,文件包含等多种高危漏洞 */ $url_arr= ...

  9. 【leetcode❤python】235. Lowest Common Ancestor of a Binary Search Tree

    #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init ...

  10. Servlet与Tomcat

    Web应用不仅局限于展示在服务器上的静态页面,更多的是根据用的请求动态的生成页面信息,还可以从数据库中提取数据,生成页面返回给用户. 第一种方法:遵循HTTP协议实现一个服务器端软件 第二种方法:利用 ...