码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA中日期处理

时间:2016-08-07 20:12:55      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

一、日期和long类型数据的相互转换

public class Hello
{
    public static void main(String[] args) throws Exception
    {
        //日期转换为long
        Date currentDateInDate = new Date();
        long currentDateInLong = currentDateInDate.getTime();
        System.out.println(currentDateInDate);
        System.out.println(currentDateInLong); 
    } 
}

二、long类型转换为日期

public class Hello
{
    public static void main(String[] args) throws Exception
    {
        //日期转换为long
        long currentDateInLong = System.currentTimeMillis();
        Date currentDateInDate = new Date(currentDateInLong);
        System.out.println(currentDateInDate);
        System.out.println(currentDateInLong); 
    } 
}

三、日期格式化(将日期format成字符串)

public class Hello
{
    public static void main(String[] args) throws Exception
    {
        //日期转换为long
        long currentDateInLong = System.currentTimeMillis();
        Date currentDateInDate = new Date(currentDateInLong);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS");
        String currentDateInString = sdf.format(currentDateInDate);
        System.out.print(currentDateInString);
    } 
}

四、日期的解析(将字符串转化为日期)

public class Hello
{
    public static void main(String[] args) throws Exception
    {
        String currentDateInString = "2016-08-07 19:24:03.0528";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS");
        Date currentDateInDate = sdf.parse(currentDateInString);
        System.out.print(currentDateInDate);
    } 
}

五、利用Calendar类获取日期的各个部分

public class Hello
{
    public static void main(String[] args) throws Exception
    {
        Calendar calendar = Calendar.getInstance();
        System.out.println("年:" + calendar.get(Calendar.YEAR));
        System.out.println("月:" + (calendar.get(Calendar.MONTH) + 1));
        System.out.println("日:" + calendar.get(Calendar.DAY_OF_MONTH));
    } 
}

 

JAVA中日期处理

标签:

原文地址:http://www.cnblogs.com/kuillldan/p/5746924.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!