SimpleDateFormat转换时间,12,24时间格式

2018-10-25 11:05:16

在使用SimpleDateFormat时格式化时间的 yyyy.MM.dd 为年月日而如果希望格式化时间为12小时制的,则使用hh:mm:ss 如果希望格式化时间为24小时制的,则使用HH:mm:ss


 

        Date d = new Date();  


        SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//12小时制  


        System.out.println(ss.format(d));  


        Date date = new Date();  


        SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制  


        String LgTime = sdformat.format(date);  


        System.out.println(LgTime);  


 


结果为  


2008-05-28 01:32:54 


2008-05-28 13:32:54 


Date d = new Date();

SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//12小时制

System.out.println(ss.format(d));

 

Date date = new Date();

SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制

String LgTime = sdformat.format(date);

System.out.println(LgTime);

 

 

结果为

2008-05-28 01:32:54

2008-05-28 13:32:54

  Date类,已经很少用了。更多使用的是Calendar   

  Calendar    date    =    Calendar.getInstance();   

  date.get(Calendar.HOUR_OF_DAY    );//得到24小时机制的   

  date.get(Calendar.HOUR);//    得到12小时机制的   


 


如下是应用calendar的时间转换类


package test;

 

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

 

 

public class Main {

 

/**

* @param args

* @throws IOException 

*/

public static void main(String[] args) throws IOException {

// TODO Auto-generated method stub

Calendar c=Calendar.getInstance();

long milliseconds=c.getTimeInMillis();

System.out.println("当前时间毫秒值:"+milliseconds);

//当前时间

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

        System.out.println("直接格式化毫秒值输出:"+sdf.format(milliseconds));

        //2011-08-20 04:27:16

        Date d=new Date(milliseconds);

        //转换成Date对象

        System.out.println("Date对象输出时间:"+sdf.format(d));

        Calendar c2=Calendar.getInstance();

        System.out.println("Calendar设置前毫秒值:"+c2.getTimeInMillis());

        c2.setTime(d);

        //通过setTime方法转换回Calendar对象

        System.out.println("Calendar设置后毫秒值:"+c2.getTimeInMillis());

}

 

}

 

--------------------- 

作者:yangshuanbao 

来源:CSDN 

原文:https://blog.csdn.net/yangshuanbao/article/details/6864054 

版权声明:本文为博主原创文章,转载请附上博文链接!

  • 2018-11-17 21:11:14

    Android中App可分配内存的大小

     结果:(1)未设定属性android:largeheap = "true"时,可以申请到的最大内存空间为221M。      (2)设定属性android:largeheap = "true"时, 可以申请的最大内存空间为478M,是原来的两倍多一些。

  • 2018-11-17 22:44:53

    LeakCanary,30分钟从入门到精通

    在性能优化中,内存是一个不得不聊的话题;然而内存泄漏,显示已经成为内存优化的一个重量级的方向。当前流行的内存泄漏分析工具中,不得不提的就是LeakCanary框架;这是一个集成方便, 使用便捷,配置超级简单的框架,实现的功能却是极为强大的。

  • 2018-11-17 22:53:01

    gc for alloc freed

    在数组中选择图片然后显示,然后。。。logcat不断显示GC回收。最后程序黑屏。

  • 2018-11-17 23:25:38

    Android高效内存1:一张图片占用多少内存

    在做内存优化的时候,我们发现除了解决内存泄露问题,剩下的就只有想办法减少真实的内存占用。而在App中,大部分内存可能被我们图片占用了,所以减少图片的内存占用可以带来直接的效果。本文就简单介绍一张图片到底占用多少内存,我们先假设我们有一张图片时 600 * 800 的,图片占用空间大小假设是 100KB。另外本文知识点也是面试官喜欢问的一个点,看看自己的回答到什么级别了。

  • 2018-11-18 09:06:06

    Android子线程中更新UI的3种方法

    UI的更新必须在主线程中完成,所以不管上述那种方法,都是将更新UI的消息发送到了主线程的消息对象,让主线程做处理。

  • 2018-11-19 15:10:23

    nodemailer的使用,nodejs发送邮件

    前段时间有个很普通的项目需要发邮件的功能,而且是刚开始学nodejs,所以只是搜索了下用什么好的库能实现,就找到了nodemailer了。这篇文章主要是记录一下使用的过程和经验。

  • 2018-11-21 09:07:37

    Android为每个应用分配多少内存?

    熟悉Android内存分配机制的朋友都知道,Android为每个进程分配内存时,采用弹性的分配方式,即刚开始并不会给应用分配很多的内存,而是给每一个进程分配一个“够用”的内存大小。

  • 2018-11-22 21:13:28

    webview之独立进程

    app内存占用大,被系统回收的概率就高,当每次把app切到后台再回到app时,可能每次app都会重启,最常见的是activity或fragment被回收了,导致fragment使用activity的数据时,出现NullPointerException。内存占用大,app越不稳定。运行性能差。webview加载页面后会占用更多的内存,从而导致app内存占用大,最终导致出现以上问题。