java 时间处理

Java 时间处理

1:格式化时间:
个人最喜欢最后一种时间结果,大多数人要的也是最后一种吧。
		package cn.com.time;

		import java.text.SimpleDateFormat;
		import java.util.Calendar;

		public class Test1 {
			public static void main(String args[]) {
				Calendar cal = Calendar.getInstance();
				SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
				SimpleDateFormat sdf2 = new SimpleDateFormat("EEE,MMM d, '' yy");
				SimpleDateFormat sdf3 = new SimpleDateFormat("h:mm a");
				SimpleDateFormat sdf4 = new SimpleDateFormat("hh 'o''clock' a,zzzz");
				SimpleDateFormat sdf5 = new SimpleDateFormat("K:mm a, z");
				SimpleDateFormat sdf6 = new SimpleDateFormat("yyyy.MMMMM.dd GGG hh:mm aaa");
				SimpleDateFormat sdf7 = new SimpleDateFormat("EEE,d MMM yyyy HH:mm:ss Z");
				SimpleDateFormat sdf8 = new SimpleDateFormat("yyMMddHHmmssZ");
				SimpleDateFormat sdf9 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
				SimpleDateFormat sdf10 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				System.out.println(sdf1.format(cal.getTime()));
				System.out.println(sdf2.format(cal.getTime()));
				System.out.println(sdf3.format(cal.getTime()));
				System.out.println(sdf4.format(cal.getTime()));
				System.out.println(sdf5.format(cal.getTime()));
				System.out.println(sdf6.format(cal.getTime()));
				System.out.println(sdf7.format(cal.getTime()));
				System.out.println(sdf8.format(cal.getTime()));
				System.out.println(sdf9.format(cal.getTime()));
				System.out.println(sdf10.format(cal.getTime()));
			}
		}
结果:
2018.07.07 公元 at 08:23:09 CST
周六,7月 7, ' 18
8:23 上午
08 o'clock 上午,中国标准时间
8:23 上午, CST
2018.七月.07 公元 08:23 上午
周六,7 7月 2018 08:23:09 +0800
180707082309+0800
2018-07-07T08:23:09.858+0800
2018-07-07 08:23:09

2.获取当前时间:
		package cn.com.time;

		import java.text.SimpleDateFormat;
		import java.util.Calendar;

		public class Test1 {
			public static void main(String args[]) {
				Calendar cal=Calendar.getInstance();
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				System.out.println("当前时间是:"+sdf.format(cal.getTimeInMillis()));
			}
		}
结果:
当前时间是:2018-07-07 08:30:53

3.:获取年份,月份
		package cn.com.time;

		import java.util.Calendar;

		public class Test1 {
			public static void main(String args[]) {
				Calendar cal=Calendar.getInstance();
				System.out.println("时间戳"+cal.getTimeInMillis());
				System.out.println("当前时间:"+cal.getTime());
				System.out.println("当前哪一年:"+cal.get(cal.YEAR));
				System.out.println("当前哪一月:"+(cal.get(cal.MONTH)+1));
				System.out.println("这一年中的第几天:"+cal.get(cal.DAY_OF_YEAR));
				System.out.println("当前月中的第几天:"+cal.get(cal.DATE));
				System.out.println("当前星期中的第几天:"+cal.get(cal.DAY_OF_WEEK));

			}
		}
结果:
时间戳1530925414767
当前时间:Sat Jul 07 09:03:34 CST 2018
当前哪一年:2018
当前哪一月:7
这一年中的第几天:188
当前月中的第几天:7
当前星期中的第几天:7

4:转换时间戳:
		package cn.com.time;

		import java.text.SimpleDateFormat;
		import java.util.Calendar;

		public class Test1 {
			public static void main(String args[]) {
				Calendar cal=Calendar.getInstance();
				long time1=cal.getTimeInMillis();
				long time2=System.currentTimeMillis();
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				System.out.println(sdf.format(time1));
				System.out.println(sdf.format(time2));

			}
		}
结果:
2018-07-07 09:07:24
2018-07-07 09:07:24

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值