zoukankan      html  css  js  c++  java
  • 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分别表示从星期日,星期一,。。。,星期六
  • 相关阅读:
    Windows 8.1 应用开发文章汇总
    演绎事件委托
    Azure China
    Orchard 刨析:导航篇
    为ASP.NET MVC应用程序使用高级功能
    兼容Mono的下一代云环境Web开发框架ASP.NET vNext
    商品导航菜单
    纳斯达克对经济泡沫的定义是:当交易价格远大于内在价值的时候,就称为泡沫(转)
    当一个低级问题,第一次解决时,你会感受到成就;第二次解决时,你感受到责任,第三次解决时,你可能更多的感受到无力(转)
    互联网组织的未来:剖析GitHub员工的任性之源(转)
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/5579146.html
Copyright © 2011-2022 走看看