java - 将日期时间转换为24小时表单

我从服务器获得的时间就像Jul 27, 2011 8:35:29 AM。

我想将其转换为2011-07-27 08:35:29。

我还希望转换时间为24小时格式。 任何人都可以解决这个问题。 我想得到的输出就像2011-07-27 08:35:29

Sujiz asked 2019-06-10T23:04:50Z

12个解决方案

142 votes

H vs h是24小时与12小时格式之间的差异。

lwpro2 answered 2019-06-10T23:05:21Z

104 votes

试试这个:

String dateStr = "Jul 27, 2011 8:35:29 AM";

DateFormat readFormat = new SimpleDateFormat( "MMM dd, yyyy hh:mm:ss aa");

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

Date date = null;

try {

date = readFormat.parse(dateStr);

} catch (ParseException e) {

e.printStackTrace();

}

if (date != null) {

String formattedDate = writeFormat.format(date);

}

Mark Allison answered 2019-06-10T23:04:57Z

78 votes

kk = Hours in 1-24 format

hh= hours in 1-12 format

KK= hours in 0-11 format

HH= hours in 0-23 format

Savas Adar answered 2019-06-10T23:05:38Z

10 votes

试试这个:

Date date=new Date("12/12/11 8:22:09 PM");

System.out.println("Time in 24Hours ="+new SimpleDateFormat("HH:mm").format(date));

Jaadu answered 2019-06-10T23:05:57Z

5 votes

您应该看一下Java日期格式,特别是SimpleDateFormat。 这里有一些例子:[http://download.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html] - 但你也可以通过快速谷歌找到更多。

stephendnicholas answered 2019-06-10T23:06:21Z

4 votes

我以下面给出的日期为例,根据您的要求打印两种不同的日期格式。

String date="01/10/2014 05:54:00 PM";

SimpleDateFormat simpleDateFormat=new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa",Locale.getDefault());

try {

Log.i("",""+new SimpleDateFormat("ddMMyyyyHHmmss",Locale.getDefault()).format(simpleDateFormat.parse(date)));

Log.i("",""+new SimpleDateFormat("dd/MM/yyyy HH:mm:ss",Locale.getDefault()).format(simpleDateFormat.parse(date)));

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

如果您还有任何疑问,请回复。 谢谢。

droidd answered 2019-06-10T23:06:52Z

2 votes

分为两行:

SimpleDateFormat simpleDateFormat=new SimpleDateFormat("hh:mm aa",Locale.getDefault());

FAJR = new SimpleDateFormat("HH:mm",Locale.getDefault()).format(simpleDateFormat.parse("8:35 PM");

meduvigo answered 2019-06-10T23:07:16Z

1 votes

只是一个提示,

尝试使用JodaTime而不是java,util.Date它更强大,它有一个方法toString(“”),您可以传递您想要的格式toString(“yyy-MM-dd HH:mm:ss”);

[http://joda-time.sourceforge.net/]

fredcrs answered 2019-06-10T23:07:53Z

1 votes

import java.text.SimpleDateFormat;

import java.util.Date;

public class DateFormatExample {

public static void main(String args[]) {

// This is how to get today's date in Java

Date today = new Date();

//If you print Date, you will get un formatted output

System.out.println("Today is : " + today);

//formatting date in Java using SimpleDateFormat

SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy");

String date = DATE_FORMAT.format(today);

System.out.println("Today in dd-MM-yyyy format : " + date);

//Another Example of formatting Date in Java using SimpleDateFormat

DATE_FORMAT = new SimpleDateFormat("dd/MM/yy");

date = DATE_FORMAT.format(today);

System.out.println("Today in dd/MM/yy pattern : " + date);

//formatting Date with time information

DATE_FORMAT = new SimpleDateFormat("dd-MM-yy:HH:mm:SS");

date = DATE_FORMAT.format(today);

System.out.println("Today in dd-MM-yy:HH:mm:SS : " + date);

//SimpleDateFormat example - Date with timezone information

DATE_FORMAT = new SimpleDateFormat("dd-MM-yy:HH:mm:SS Z");

date = DATE_FORMAT.format(today);

System.out.println("Today in dd-MM-yy:HH:mm:SSZ : " + date);

}

}

输出:

今天是:2012年1月2日星期五16:11:27 IST 2012

今天以dd-MM-yyyy格式:02-11-2012

今天以dd / MM / yy模式:02/11/12

今天在dd-MM-yy:HH:mm:SS:02-11-12:16:11:316

今天在dd-MM-yy:HH:mm:SSZ:02-11-12:16:11:316 +0530

Nilesh Jadav answered 2019-06-10T23:08:45Z

1 votes

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

Date date = new Date();

Date date2 = new Date("2014/08/06 15:59:48");

String currentDate = dateFormat.format(date).toString();

String anyDate = dateFormat.format(date2).toString();

System.out.println(currentDate);

System.out.println(anyDate);

Anahit Serobyan answered 2019-06-10T23:09:02Z

0 votes

这是一种使用JODA的方法。

输入示例是07/20/2015 01:46:34.436 AM

public static String get24HourFormat(String dateString){

Date date = new Date();

DateTime date1=new DateTime();

// String date="";

int hour=0;

//int year=0;

if(dateString!=null){

try {

DateTimeFormatter formatter = DateTimeFormat.forPattern("MM/dd/yyyy hh:mm:ss.SSS aa");

DateTimeFormatter output = DateTimeFormat.forPattern("kk");

date1=formatter.parseDateTime(dateString);

hour=date1.getHourOfDay();

System.out.println(output.print(date1));

// System.out.println(date);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return hour+"";

}

user1930402 answered 2019-06-10T23:09:33Z

0 votes

使用Calendar,其工作方式如下:

//create first Calendar object

Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.HOUR_OF_DAY, 15); // 3 PM

// the same is

calendar.set(Calendar.AM_PM, Calendar.PM); // choose PM mode

calendar.set(Calendar.HOUR, 3); // 3 PM

System.out.println( calendar.get(Calendar.HOUR) ); // AM_PM format

System.out.println( calendar.get(Calendar.HOUR_OF_DAY) ); // 0-23 format

Andreas L. answered 2019-06-10T23:09:57Z

date格式化输出 24小时 java_java - 将日期时间转换为24小时表单相关推荐

  1. android时间24小时,安卓时间显示TextClock显示日期时间,24小时制和12小时制(自定义...

    安卓时间显示TextClock显示日期时间,24小时制和12小时制(自定义 安卓时间显示TextClock显示日期时间,24小时制和12小时制(自定义时间) 这个地方的是按24小时制还是12小时制 是 ...

  2. 时间的坑yyyy-MM-dd HH:mm:ss代表将时间转换为24小时制

    java中的的日期格式为: yyyy-MM-dd HH:mm:ss:代表将时间转换为24小时制,例: 2018-06-27 15:24:21 yyyy-MM-dd hh:mm:ss:代表将时间转换为1 ...

  3. oracle 截取时间至小时,如何在pandas中构造/取整到小时的本地化日期时间列

    我想把pandas python中本地化的datetime列截短/四舍五入到一小时.例如,如果我有2017-10-15 15:03:25+02:00,我希望获得2017-10-15 15 15:00: ...

  4. 字符串类型日期时间转换为Date类型解析转换异常java.text.ParseException: Unparseable date: “2019-09-27T18:31:31+08:00”

    字符串类型日期时间转换为Date类型解析转换异常java.text.ParseException: Unparseable date: "2019-09-27T18:31:31+08:00& ...

  5. MySQL关于把AM/PM格式的时间转换为24小时制的时间格式

    MySQL把AM/PM格式的时间转换为24小时制的时间格式 时间无序表达时,使用STR_TO_DATE函数进行转换,具体示例如下: select 交易时间,STR_TO_DATE(交易时间,'%m/% ...

  6. 将UTC日期时间转换为本地日期时间

    本文翻译自:Convert UTC date time to local date time From the server I get a datetime variable in this for ...

  7. vue日期时间转换为年月日格式

    vue日期时间转换为年月日格式 const dateFormat = (val, type) => {let time = ''if (val !== null) {const date = n ...

  8. Matlab:将日期时间转换为儒略日期或 POSIX 时间

    Matlab:将日期时间转换为儒略日期或 POSIX 时间 在转换之前指定时区 将已设置时区和未设置时区的日期时间值转换为儒略日期 将已设置时区和未设置时区的日期时间值转换为 POSIX 时间 您可以 ...

  9. java date格式化输出_Java Date类以及日期的格式化输出

    Java中的Date类用于表示日期时间,在java.util包中.虽然Date类在设计之初没有考虑到国际化,很多方法都已经被定义为过时,但是Date却是程序设计过程中经常用到的一个类.本文将说说Dat ...

最新文章

  1. 2016政策与市场协同发力大数据,小公司如何搏杀BAT?
  2. 线程的局部变量ThreadLocal概念
  3. 设计灵感|色彩叠加在海报设计中的妙用!
  4. WebService 出现因 URL 意外地以“/HelloWorld”结束,请求格式无法识别。
  5. axios.all()解决并发请求的问题
  6. Trick(十二)——统计 label(序列)各个标签值出现的次数以及出现次数最多的标签
  7. 机器学习、统计分析、数据挖掘、神经网络、人工智能、模式识别,
  8. C# Web Service 不使用服务引用直接调用方法(转)
  9. 社区团购小程序开发安装教程(基于yii框架+PHP)
  10. 2018年阿里巴巴前端开源项目汇总
  11. R中两种常用并行方法——2. snowfall
  12. 集成VueCli5各种功能与插件(一)安装
  13. 解决:tcpdump -w xxxxx.pcap 提示 Permission denied
  14. 51单片机实验之流水灯和交替闪烁
  15. 英语读书笔记-Book Lovers Day 11
  16. C++ 代码模拟登录淘宝、天猫、支付宝等电商网站的实现
  17. Verilog系统函数
  18. 使用Heritrix进行主题抓取
  19. Microsoft详细介绍了OPSEC,SolarWinds黑客使用的取证技术
  20. 铜和不锈钢的焊接方法

热门文章

  1. HTTP协议调试工具汇总,你心目中应该是什么样的?
  2. 看《墨攻》理解IoC概念(二 )http://www.qqread.com/erp/3/j380307_3.html
  3. 毕达哥斯拉三元组poj1305
  4. android opencv抠图
  5. SQLServer乱码问题的分析及解决方法(中文字符被存入数据库后,显示为乱码)
  6. html虚拟打印转为pdf,Doro PDF Writer 虚拟打印机 – 可将任何格式图片/文档/文件转换成 PDF格式...
  7. 苹果App Store申请和管理
  8. 在使用计算机时 若直接通过断点,计算机等级考试三级PC技术练习题汇总(共6章)...
  9. 来了!Goby一年一度的红队专版正式发布!!
  10. Lnmp环境搭建及配置