Date、SimpleDateFormat日期类和Calender类
来源:互联网 发布:美国用什么软件买东西 编辑:程序博客网 时间:2024/04/28 21:11
Date date = new Date(); System.out.println(date); //返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。 System.out.println(date.getTime()); Long d = 1486539701857l; //date.setTime(d); //表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的时间点。 System.out.println(date); //格式化时间,注意DateFormat是一个抽象类,并且有些方法是静态的 DateFormat df1= null; DateFormat df2=null; DateFormat df3=null; df1=DateFormat.getDateInstance();//获取日期格式器,该格式器具有默认语言环境的默认格式化风格 df3=DateFormat.getInstance(); df2=DateFormat.getDateTimeInstance(); //格式化日期和时间输出 System.out.println("Date:"+df1.format(date));//Date:2017-2-8 System.out.println("Time:"+df3.format(date));//Time:17-2-8 下午4:01 System.out.println("TimeDate:"+df2.format(date));//TimeDate:2017-2-8 16:27:07 /** * static DateFormat: getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale) 获取日期/时间格式器,该格式器具有给定语言环境的给定格式化风格。 */ df1=DateFormat.getDateInstance(DateFormat.FULL,new Locale("zh", "CN")); System.out.println("中国人喜欢的格式:"+df1.format(date));//:2017年2月8日 星期三 df2=DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL,new Locale("zh", "CN")); System.out.println(df2.format(date));//2017年2月8日 星期三 下午04时27分07秒 CST //SimpleDateFormat 可以自己定义时间格式 String str = "2017-03-19 17:18:30"; Date date2 = null; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 HH-mm-ss"); try { //Date java.text.DateFormat.parse(String source) throws ParseException date2=sdf.parse(str); } catch (Exception e) { // TODO: handle exception } System.out.println(date2); String string=sdf1.format(date2); System.out.println(string); Calendar calendar= Calendar.getInstance(); System.out.println(calendar); System.out.println(calendar.get(Calendar.YEAR)); System.out.println(calendar.get(Calendar.MONTH)+1); System.out.println(calendar.get(Calendar.DATE)); System.out.println(calendar.get(Calendar.HOUR)); System.out.println(calendar.get(Calendar.MINUTE)); System.out.println(calendar.get(Calendar.SECOND)); calendar.set(Calendar.YEAR, 2018); System.out.println(calendar.get(Calendar.YEAR)); // 或者另一種設置 calendar 方式 // 分別爲 year, month, date, hourOfDay, minute, second calendar = Calendar.getInstance(); calendar.set(2013, 1, 2, 17, 35, 44); str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime()); System.out.println(str);
0 0
- Date、SimpleDateFormat日期类和Calender类
- Date、Calender和SimpleDateFormat类
- Java 日期类的使用:Date,Calender,SimpleDateFormat的简单使用
- 日期操作方法(总结Date,Calender类)
- 日期操作方法(总结Date,Calender类)
- Date, Calender, SimpleDateFormat
- 日期类 Date SimpleDateFormat Calendar
- JAVA中的Date日期类,和SimpleDateFormat类的使用
- Java基础 日期Date类和SimpleDateFormat类
- 日期操作类(Date、Calendar、DateFormat、SimpleDateFormat)
- Date类和SimpleDateFormat类
- JAVA API Date SimpleDateFormat and Calender
- Java中Date类和Calender类的比较
- Java中Date和Calender类的使用方法
- Java中Date和Calender类的使用方法
- Java中Date和Calender类的使用方法
- Java中Date和Calender类的使用方法
- JAVA学习笔记——时间日期类Date和SimpleDateFormat类
- 动态添加Fragment及侧滑菜单SlidingMenu仿QQ界面
- Vue.js 系列教程 ①
- Android单排上王者系列之Handler机制源码分析
- [leetcode-二叉树层次遍历并统计每层节点数]--107. Binary Tree Level Order Traversal II
- python map reduce函数初学
- Date、SimpleDateFormat日期类和Calender类
- 第三届蓝桥杯软件类省赛真题-C-A-2_古堡算式
- Hibernate 报错:Batch update returned unexpected row count from update [0]; actual row count
- 第三届蓝桥杯软件类省赛真题-C-C-1_微生物增殖
- 关于FTP服务器上传下载的代码中文乱码的解决方法
- SVN服务器搭建及客户端使用的基本流程
- WorkerMan学习篇:websocket+workerman聊天功能(二):同步在线用户列表
- 窗口销毁的相关函数与消息,ActiveX控件的销毁
- 安卓代码动态切换APP启动图标