package com.controller;
import java.text.SimpleDateFormat;
import java.util.Date;
public class time {
public static void main(String[] agrs) throws Exception{
//时间字符串转换格式
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = "2015-02-09 10:15:24";
Date date = simpleDateFormat.parse(str);
//Date date = new Date();
int year = Integer.parseInt(String.format("%tY",date));
int month = Integer.parseInt(String.format("%tm",date));
int day = Integer.parseInt(String.format("%td",date));
int hour = Integer.parseInt(String.format("%tH",date));
int minute = Integer.parseInt(String.format("%tM",date));
int second = Integer.parseInt(String.format("%tS",date));
System.out.println("年:"+year+"\t月:"+month+"\t日:"+day+
"\t时:"+hour+"\t分:"+minute+"\t秒:"+second);
}
}
Java获取指定日期的年、月、日、时、分和秒
最新推荐文章于 2024-04-25 11:07:58 发布