Java中時間

來源:互聯網
上載者:User

標籤:text   for   util   seda   ted   xxx   描述   imei   lis   

/**

*觀看API文檔學習:Date中很多方法失效;Calendar頂替。

*

**/
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateDemo {
    public static void main(String[] args) throws ParseException {
       /**
        * 計算 "2012/3/17" "2012-5-28"間隔天數
        * 1.毫秒值擷取相減
        * 2.擷取毫秒值,字串--》日期對象-->毫秒值
        */
       getDays();
    }
    public static void getSeconds(){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = df .parse("2017-06-25");
        //Date date1=new Date();
        Date date1 = df.parse("2017-06-24");
        Calendar cal = Calendar.getInstance();
        Calendar cal1=Calendar.getInstance();
        cal.setTime(date);
        cal1.setTime(date1);
        long timestamp = cal.getTimeInMillis();
        long timestamp1=cal1.getTimeInMillis();
        System.out.println(timestamp1-timestamp);       
    }  
    public static void getDays() throws ParseException{     
        String str_date1="2012/3/17";
        String str_date2="2012-5-28";
        //日期格式字串解析成日期對象
        //1.自訂yyyy/MM/dd風格對象
        DateFormat dateFormat1=new SimpleDateFormat("yyyy/MM/dd");
        //2.定義一個預設風格
        DateFormat dateFormat2=DateFormat.getDateInstance(DateFormat.MEDIUM);
           //查錯誤驗證  如果系統為英文預設是yy/mm/dd
            Date date=new Date();
            String str_date=dateFormat2.format(date);
            System.out.println(str_date);
        
        //3.對日期格式字串進行解析
        Date date1=dateFormat1.parse(str_date1);
        Date date2=dateFormat2.parse(str_date2);
        //4.通過日期對象擷取毫秒值
         long time1=date1.getTime();
         long time2=date2.getTime();
         //5.相減
         long time=Math.abs(time2-time1);
         //6.毫秒轉day
         int day=(int) (time/1000/60/60/24);
        System.out.println(day);

    }
    public static void cal(){
        //示範日曆
        Calendar ca=Calendar.getInstance();
    //    System.out.println(ca);
        //日曆:將時間相關的東西以索引值對的方式全部封裝在一個map集合
        /*
         *
         * java.util.GregorianCalendar[time=1497856585181,areFieldsSet=true,
         * areAllFieldsSet=true,lenient=true,
         * zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",
         * offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],
         * firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2017,MONTH=5,WEEK_OF_YEAR=25,
         * WEEK_OF_MONTH=4,DAY_OF_MONTH=19,DAY_OF_YEAR=170,DAY_OF_WEEK=2,
         * DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=3,
         * HOUR_OF_DAY=15,MINUTE=16,SECOND=25,MILLISECOND=181,ZONE_OFFSET=28800000,DST_OFFSET=0]
         *
         */
        //對日曆對象其中的日期進行定義
          //ca.set(Calendar.YEAR, 2015);
          //ca.set(2012, 2, 2);
        //日期位移
        //ca.add(Calendar.YEAR, -4);
        //日期位移比較狠
        //ca.add(Calendar.MONTH, 7);
        //showDate(ca);
        //任意一年2月有多少天?3月1日的前一天。
       //int year=2014;
       //ca.set(year, 2,1);
       //ca.add(Calendar.DAY_OF_MONTH, -1);
      // showDate(ca);
       //擷取昨天的現在時刻。
        ca.add(Calendar.DAY_OF_MONTH, -1);
        showDate(ca);
    }
    public static void showDate(Calendar ca){ 
        int year=ca.get(Calendar.YEAR);
        int month=ca.get(Calendar.MONTH)+1;
        int day=ca.get(Calendar.DAY_OF_MONTH);
        int week=ca.get(Calendar.DAY_OF_WEEK);
        System.out.println(year+"年"+month+"月"+day+"日"+week);
        
    }
    public static void date() {
        Date date = new Date();
        System.out.println(date);
        // 格式化--日期轉文本 解析--文本轉日期
        // DateFormat 提供了很多類方法,以獲得基於預設或給定語言環境和多種格式化風格的預設日期/時間 Formatter。
        // 格式化風格包括 FULL、LONG、MEDIUM 和 SHORT。方法描述中提供了使用這些風格的更多細節和樣本。
        // 通過DateFormat類中的靜態Factory 方法擷取執行個體

        DateFormat df = DateFormat.getDateInstance();

        // 如果格式一般我們還可以加入style
        df = DateFormat.getDateInstance(DateFormat.SHORT);
        // FULL:2017年6月19日 星期一
        // Long:2017年6月19日
        // MEDIUM:2017-6-19
        // SHORT:17-6-19
        // 轉成自訂格式 XXXX/XX/XX 只能自訂對象 .使用子類//去SimpleDateFormat類總找日期和時間模式

        df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");

        // 日期+時間
        DateFormat df1 = DateFormat.getDateTimeInstance(DateFormat.FULL,
                DateFormat.FULL);
        String dateformat1 = df1.format(date);
        System.out.println(dateformat1);

        // 使用DateFormat的format方法對日期對象進行格式化

        String dateformat = df.format(date);
        System.out.println(dateformat);
    }
    public static void date2() {
        // 毫秒值--》日期對象 建構函式 setTime方法
        long time = System.currentTimeMillis();
        System.out.println(time);
        Date date = new Date(time);
        System.out.println(date);
        DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
        String df_date = df.format(date);
        System.out.println(df_date);
        // 日期對象--》毫秒值 計算時間差 (時間戳記)
        long time1 = date.getTime();
        System.out.println(time1);
    }
    public static void date1() throws ParseException {
        Date date = new Date();
        System.out.println(date);// 後台輸出系統時間 但是格式看不懂
        // 格式轉化 xxxx-yy-mm
        String str = "2016-09-07";
        DateFormat df = DateFormat.getDateInstance();

        Date date1 = df.parse(str);
        System.out.println(date1);
    }
}

Java中時間

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.