Java中Date日期格式的各种转换
来源:互联网 发布:淘宝代运营合作 编辑:程序博客网 时间:2024/06/06 07:48
- public class DateParserT {
- /**
- * Date 与 String、long 的相互转换
- * @param args
- */
- public static void main(String[] args) {
- Date dt =new Date();
- System.out.println(dt); //格式: Wed Jul 06 09:28:19 CST 2016
- //格式:2016-7-6
- String formatDate = null;
- formatDate = DateFormat.getDateInstance().format(dt);
- System.out.println(formatDate);
- //格式:2016年7月6日 星期三
- formatDate = DateFormat.getDateInstance(DateFormat.FULL).format(dt);
- System.out.println(formatDate);
- //格式 24小时制:2016-07-06 09:39:58
- DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //HH表示24小时制;
- formatDate = dFormat.format(dt);
- System.out.println(formatDate);
- //格式12小时制:2016-07-06 09:42:44
- DateFormat dFormat12 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); //hh表示12小时制;
- formatDate = dFormat12.format(dt);
- System.out.println(formatDate);
- //格式去掉分隔符24小时制:20160706094533
- DateFormat dFormat3 = new SimpleDateFormat("yyyyMMddHHmmss");
- formatDate = dFormat3.format(dt);
- System.out.println(formatDate);
- //格式转成long型:1467770970
- long lTime = dt.getTime() / 1000;
- System.out.println(lTime);
- //格式long型转成Date型,再转成String: 1464710394 -> ltime2*1000 -> 2016-05-31 23:59:54
- long ltime2 = 1464710394;
- SimpleDateFormat lsdFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date lDate = new Date(ltime2*1000);
- String lStrDate = lsdFormat.format(lDate);
- System.out.println(lStrDate);
- //格式String型转成Date型:2016-07-06 10:17:48 -> Wed Jul 06 10:17:48 CST 2016
- String strDate = "2016-07-06 10:17:48";
- SimpleDateFormat lsdStrFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- try {
- Date strD = lsdStrFormat.parse(strDate);
- System.out.println(strD);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- }
- }
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timeStr = sdf.format(new Date());
System.out.println("当前时间为:"+timeStr);
String fuck = timeStr.substring(5,timeStr.length());
System.out.println(fuck);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年M月d日");
try {
Date d = sdf2.parse("2013年01月06日");
sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(d));
System.out.println(d.getDate());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf3.parse("2017-09-01");
sdf3=new SimpleDateFormat("MM-dd");
String shit = sdf3.format(date);
System.out.println(shit);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
- Java中Date日期格式的各种转换
- Java中Date日期格式的各种转换
- Java中Date日期格式的各种转换
- Java中Date日期格式的各种转换
- Java中Date日期格式的各种转换
- java 中Date的各种格式转换
- java中string与date(日期)格式之间的转换
- java中string与date(日期)格式之间的转换
- java中string与date(日期)格式之间的转换
- java中string与date(日期)格式之间的转换
- java中string与date(日期)格式之间的转换
- java中string与date(日期)格式之间的转换
- Oracle中Date的各种格式转换
- java Date日期格式转换
- DateTime中各种日期格式的转换
- Java 日期格式的各种转换
- java中各种date格式的转化
- java中各种date格式的转化
- 嵌入式系统软件-4.4
- 对utf-8编码的简单解析
- 数组和数组的反转
- vagrant lnmp css 缓存 乱码
- 买票系统模拟Java高并发(ConcurrentQueue的解决方案)
- Java中Date日期格式的各种转换
- sqlite3命令
- luogu #2680 运输计划(二分答案+树上差分)(noip2015)
- MapReduce Join
- [leetcode] 11 Container With Most Water
- 如何变得有思想
- [论文翻译]Pedestrian Alignment Network for Large-scale Person Re-Identification
- 阿里云优惠码在哪里看?———详解阿里云优惠码和幸运券的使用技巧
- 使用hex编码绕过主机卫士IIS版本继续注入