天天看点

Java中时间的处理

1、格式化时间(SimpleDateFormat)

如何使用 SimpleDateFormat 类的 format(date) 方法来格式化时间?

复制代码

package Java_Learn.File;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main1 {

public static void main(String[] args) {

Date date = new Date();

String strDateFormat = "yyyy-MM-dd HH:mm:ss";

String msg="当前时间是:";

formatTime(msg,date,strDateFormat);

}

private static void formatTime(String msg,Date date,String strDateFormat)

{

SimpleDateFormat simpleDateFormat=new SimpleDateFormat(strDateFormat);

System.out.println(msg+simpleDateFormat.format(date));

}

}

2、当前时间

如何获取当前时间?

复制代码

package Java_Learn.File;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main1 {

public static void main(String[] args) {

Date date = new Date();

String strDateFormat = "yyyy-MM-dd HH:mm:ss";

String msg="当前时间是:";

formatTime(msg,date,strDateFormat);

formatTime1(msg,date,strDateFormat);

}

private static void formatTime(String msg,Date date,String strDateFormat)

{

SimpleDateFormat simpleDateFormat=new SimpleDateFormat(strDateFormat);

System.out.println(msg+simpleDateFormat.format(date));

}

private static void formatTime1(String msg,Date date,String strDateFormat)

{

SimpleDateFormat simpleDateFormat=new SimpleDateFormat(strDateFormat);

simpleDateFormat.applyPattern(strDateFormat);// a为am/pm的标记

System.out.println(msg+simpleDateFormat.format(date));

}

}

3、年、月、日等时间的处理

如何获取年、月、日等时间格式?

复制代码

import java.util.Calendar;

public class Main {

public static void main(String[] args) {

Calendar cal = Calendar.getInstance();

int day = cal.get(Calendar.DATE);

int month = cal.get(Calendar.MONTH) + 1;

int year = cal.get(Calendar.YEAR);

int dow = cal.get(Calendar.DAY_OF_WEEK);

int dom = cal.get(Calendar.DAY_OF_MONTH);

int doy = cal.get(Calendar.DAY_OF_YEAR);

System.out.println("当期时间: " + cal.getTime());
    System.out.println("日期: " + day);
    System.out.println("月份: " + month);
    System.out.println("年份: " + year);
    System.out.println("一周的第几天: " + dow);  // 星期日为一周的第一天输出为 1,星期一输出为 2,以此类推
    System.out.println("一月中的第几天: " + dom);
    System.out.println("一年的第几天: " + doy);
}
           

}

复制代码

结果:

复制代码

当期时间: Fri Mar 27 21:44:15 CST 2015

日期: 27

月份: 3

年份: 2015

一周的第几天: 6

一月中的第几天: 27

一年的第几天: 86

复制代码

4、时间戳转换成时间

如何使用 SimpleDateFormat 类的 format() 方法将时间戳转换成时间。

日期和时间模式(注意大小写,代表的含义是不同的):

yyyy:年

MM:月

dd:日

hh:1~12小时制(1-12)

HH:24小时制(0-23)

mm:分

ss:秒

S:毫秒

E:星期几

D:一年中的第几天

F:一月中的第几个星期(会把这个月总共过的天数除以7)

w:一年中的第几个星期

W:一月中的第几星期(会根据实际情况来算)

a:上下午标识

k:和HH差不多,表示一天24小时制(1-24)

K:和hh差不多,表示一天12小时制(0-11)

z:表示时区

复制代码

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main{

public static void main(String[] args){

Long timeStamp = System.currentTimeMillis(); //获取当前时间戳

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

String sd = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp)))); // 时间戳转换成时间

System.out.println("格式化结果:" + sd);

SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy 年 MM 月 dd 日 HH 时 mm 分 ss 秒");
    String sd2 = sdf2.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
    System.out.println("格式化结果:" + sd2);
           

}

}