java 日期与时间类
1.Date类: https://www.cnblogs.com/huangminwen/p/5994927.html
2.DateFormat和SimpleDateFormat (simple简单的)
DateFormat类
此类是一个日期的格式化类,用来格式化日期。具体日期可以通过java.util.Date类来获取。
DateFormat类的定义:此类是定义在java.test包中的。
public abstract class DateFormat extends Format
1
实例化DateFormat
DateFormat是一个抽象类,按照以住的思路,直接使用其子类实例化即可。但是DateFormat 类本身的内部提供了可以直接为其实例化的操作。
//得到日期的DateFormat对象: public static final DateFormat getDateInstance();
//得到日期时间的DateFormat对象: public static final DateFormat getDateTimeInstance();
//使用DateFormat类格式化Date类日期 public final String format(Date date)
SimpleDateFormat类使用
在构造对象时要传入日期格式模板
//构造方法: public SimpleDateFormat(String pattern)
//转换: public Date parse(String source)throws ParseException //-->此时取得的是全部时间数。
//格式化: public final String Format(Date date) //-->将时间重新格式化成字符串显示。
import java.text.SimpleDateFormat;
import java.util.Date; public class Lianxi { public static void main(String[] args) {
SimpleDateFormat date1 = new SimpleDateFormat("yy/MM/dd");
Date now = new Date();
SimpleDateFormat date2 = new SimpleDateFormat(
"一年中的第 D 天 一年中第w个星期 一月中第W个星期 在一天中k时 z时区");
System.out.println(date1.format(now));//19/02/03
System.out.println(date2.format(now));//一年中的第 34 天 一年中第6个星期 一月中第2个星期 在一天中15时 CST时区
System.out.println(now.toGMTString());//格林时间-3 Feb 2019 07:02:48 GMT
System.out.println(now.toLocaleString());//2019-2-3 15:02:48
System.out.println(now.toString());//Sun Feb 03 15:02:48 CST 2019
}
}
String和Date的转换
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner; public class TestDateFommat {
public static void main(String[] args) throws ParseException {
DateFormat d=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
/**Date-->String format(Date d)
* String-->Date parse(String s)*/
Date date=new Date(12318231233123L);
System.out.println(date);
String strDate=d.format(date);
System.out.println(strDate); Scanner input=new Scanner(System.in);
System.out.println("请输入一个时间:yyyy-MM-dd HH:mm:ss.SSS");
String str=input.nextLine();
Date d2=d.parse(str); System.out.println(d2);
}
}
3.日历类 Calendar :https://www.cnblogs.com/huangminwen/p/6041168.html
最新文章
- 使用NW.js封装微信公众号菜单编辑器为桌面应用
- Git相关文章
- SQL Server:“数据收缩”详解
- 虚拟机中如何Linux系统如何访问PC硬盘中的文件(如何将windows下的文件夹挂载到linux虚拟机下)
- 盛希泰:办公室就像男人的春药——人的一生的精力是有限的,你把有限的时间分配给谁决定你的成败——你有N多选择,你人生的积累就是N多选择加起来的结果
- Java面试题集(136-150)
- linux cat more less head tail
- js函数验证方式:验证是否是数字,支持小数,负数
- zabbix优化,配合文件,zabbix_get命令
- ibatis项目启动报错The string ";--"; is not permitted within comments【原】
- MyBatis - 10.MyBatis扩展
- [HDU3726]Graph and Queries
- servlet 中处理 json 请求,并访问 service 类,返回处理结果
- SQL Server 之 附加数据库出现“ 拒绝访问 ”
- eclipse 自动生成json格式的toString()方法
- Django基础九之中间件
- POJ2195:Going Home(费用流入门)
- [吴恩达机器学习笔记]13聚类K-means
- Ubuntu16.04安装印象笔记
- InnoSetup 打包Winform程序
热门文章
- centos7.4+mysql5.6+virtualenv+python3.6+nginx+uwsgi+django生产环境搭建
- csharp: using HtmlAgilityPack and ScrapySharp reading Url find text
- webpack单独打包一个less文件
- VUE CLI 3.0 项目引入 ElementUI
- Java并发编程(四)synchronized
- unicode编码和utf8编码的区别
- (网页)the server responded with a status of 403 (Forbidden)
- MySQL复制ERROR 1794 (HY000): Slave is not configured or failed to initialize properly.
- [cb]ScriptableWizard 创建向导
- 11LaTeX学习系列之---LaTeX的特殊字符