Java·日期时间处理

时间:2022-05-03
本文章向大家介绍Java·日期时间处理,主要内容包括本文节选自《Netkiller Architect 手札》、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

本文节选自《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();

	}

}