前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java·日期时间处理

Java·日期时间处理

作者头像
netkiller old
发布2018-03-05 17:45:30
1.2K0
发布2018-03-05 17:45:30
举报
文章被收录于专栏:NetkillerNetkiller

本文节选自《Netkiller Architect 手札》

1.4.3. Date

1.4.3.1. SimpleDateFormat
public static void main(String[] args) {

    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    //get current date time with Date()
    Date date = new Date();
    System.out.println(dateFormat.format(date));

    //get current date time with Calendar()
    Calendar cal = Calendar.getInstance();
    System.out.println(dateFormat.format(cal.getTime()));

}			
1.4.3.2. Timestamp
Timestamp timestamp = new Timestamp(System.currentTimeMillis()); 

Date date = new Date();       
Timestamp timestamp = new Timestamp(date.getTime());	
1.4.3.3. TimeZone
package cn.netkiller.example;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class TimeZoneTest {

	public TimeZoneTest() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		TimeZone timeZone = TimeZone.getTimeZone("Asia/Harbin");
		
		Date date = new Date();       
		Timestamp timestamp = new Timestamp(date.getTime());
		
		System.out.println(timestamp);
		
		timestamp.setHours(timestamp.getHours()+8);
		System.out.println(timestamp);
		
		simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
		System.out.println(simpleDateFormat.format(date));
		
		simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Harbin"));
		System.out.println(simpleDateFormat.format(date));
		
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.setTimeZone(timeZone);
		System.out.println(simpleDateFormat.format(calendar.getTime()));
	}

}			
1.4.3.4. String to Date
			package cn.netkiller.example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringToDate {

	public StringToDate() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = "2008-8-8 8:8:8";

		try {

			Date date = formatter.parse(dateString);
			System.out.println(date);
			System.out.println(formatter.format(date));

		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

}			
1.4.3.5. 比较两个日期与时间
			package cn.netkiller.example;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateCompare {

	public DateCompare() {
		// TODO Auto-generated constructor stub
	}

	public void fun1() throws InterruptedException {
		Date d1 = new Date();
		Thread.sleep(5000);
		Date d2 = new Date();
		if (d1.before(d2)) {
			System.out.println(String.format("%s < %s", d1.toString(), d2.toString()));
		} else {
			System.out.println(String.format("%s > %s", d1.toString(), d2.toString()));
		}
		if (d2.after(d1)) {
			System.out.println(String.format("%s > %s", d2.toString(), d1.toString()));
		}

		System.out.println(String.format("%s : %s => %d", d2.toString(), d1.toString(), d1.compareTo(d2)));
		System.out.println(String.format("%s : %s => %d", d1.toString(), d2.toString(), d2.compareTo(d1)));
	}

	public void fun2() throws InterruptedException {

		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		Date date1 = new Date();
		Date date2 = new Date();
		String s1 = dateFormat.format(date1);
		String s2 = dateFormat.format(date2);
		System.out.println(String.format("%s : %s => %d", s1, s2, s1.compareTo(s2)));

		date1 = new Date();
		Thread.sleep(5000);
		date2 = new Date();
		s1 = dateFormat.format(date1);
		s2 = dateFormat.format(date2);
		System.out.println(String.format("%s : %s => %d", s1, s2, s1.compareTo(s2)));
		System.out.println(String.format("%s : %s => %d", s2, s1, s2.compareTo(s1)));
		System.out.println();
	}

	public static void main(String[] args) throws InterruptedException {
		// TODO Auto-generated method stub
		DateCompare dateCompare = new DateCompare();
		dateCompare.fun1();
		System.out.println();
		dateCompare.fun2();

	}

}			
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2016-10-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Netkiller 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 本文节选自《Netkiller Architect 手札》
    • 1.4.3. Date
      • 1.4.3.1. SimpleDateFormat
      • 1.4.3.2. Timestamp
      • 1.4.3.3. TimeZone
      • 1.4.3.4. String to Date
      • 1.4.3.5. 比较两个日期与时间
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档