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 ...
 
随机推荐
- Andriod 按钮代码
			
package com.example.test1; import android.support.v7.app.ActionBarActivity; import android.os.Bundle ...
 - hdoj4906 Our happy ending(2014 Multi-University Training Contest 4)
			
对于一个定长(size = n)的数列a, 若其存在“位置相关”的子集(含空集)使得该子集所有元素之和为k,那么将数列a计数. 其中数列a中任一元素a[i]在[0, l]内自由取值. 数据条件0≤n, ...
 - Android  invalidate() 和 postInvalidate()的区别
			
Android提供了Invalidate方法实现界面刷新,但是Invalidate不能直接在线程中调用,因为他是违背了单线程模型:Android UI操作并不是线程安全的,并且这些操作必须在UI线程中 ...
 - php cli模式学习(PHP命令行模式)
			
http://www.jb51.net/article/37796.htm php_cli模式简介 php-cli是php Command Line Interface的简称,如同它名字的意思,就是 ...
 - 深入javascript作用域链到闭包
			
我之前用过闭包,用过this,虽然很多时候知道是这么一回事,但是确实理解上还不够深入.再一次看javascript高级程序设计这本书时,发现一起很多疑难问题竟然都懂了,所以总结一下一些理解,难免有错, ...
 - 一个Public的字段引起的,谈谈继承中的new
			
一直觉得对c#面向对象这块已经掌握的很好了,因为正常情况下字段一般我们设计成私有的,今天突然想到一个实验,如下有两个很简单的类: public class Farther { ; public vir ...
 - Struts2的标签库(三)——控制标签
			
Struts2的标签库(三) --控制标签 1.if/elseif/else标签 用于分支控制,取代JSP中的if语句,根据一个boolean(test属性的值)值判断是否进行下一步运算或者输出等. ...
 - Ecplise软件Devices看到两个相同设备问题
			
Ecplise软件Devices看到两个相同设备问题 在使用过程中,连接一台设备,在Ecplise软件的Devices界面下突然看到2个设备,如下图: 图1 解决方案:先 kill-server, 再 ...
 - uTenux——重新整理底层驱动库
			
重新整理底层驱动库 1. 整理chip.h 在chip.h文件中的07----13的宏定义设置位如下,这样我们就不用在工程配中定义sam3s4c这个宏了,为我们以后通用少了一件麻烦事. //#if d ...
 - 02.iOS开发网络篇—HTTP协议
			
iOS开发网络篇—HTTP协议 说明:apache tomcat服务器必须占用8080端口 一.URL 1.基本介绍 URL的全称是Uniform Resource Locator(统一资源定位符) ...