将12小时格式转换为24小时格式 [英] convert 12 hours format to 24 hours format

查看:118
本文介绍了将12小时格式转换为24小时格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将24小时制转换成12小时制,就像这样

I convert 24 hours format to 12 hours format like this

mycode for converting 24 hour format to 12 hour
String str1 = "2009-07-01 22:45:16 PM";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
		String s1 = null;
		s1 = (new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a"))
				.format((new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")).parse(str1));
		Date d = sdf.parse(s1);
		System.out.println(sdf.format(d));





但是我想再次将12小时格式转换为24小时格式.我尝试了很多事情,但没有任何效果.任何想法吗?





But i want to convert again 12 hour format to 24 hour format.i tried many thing but nothing was worked.any idea?

推荐答案

您可以在逆转.只要确保您使用12小时的时间格式中包含am/pm指示符,否则您就没有足够的信息.看来您的示例中已经倒退了:24小时制中有一个(冗余)"PM"指示符,但是您转换成的12小时制却没有.
You can do the same thing in reverse. Just make sure that the time format you use for 12 hours includes the am/pm indicator, because otherwise you don''t have enough information. It seems that you have got this backwards in your sample: the 24 hour time has a (redundant) ''PM'' indicator, but the 12 hour format you are converting to doesn''t.


这有效:
Date d = null;
String str1 = "2009-07-01 22:45:16";
try {
	d = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(str1);
}
catch (ParseException e) {
	e.printStackTrace();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
System.out.println(sdf.format(d));



持续12到24个小时(我不好!)



And for 12 hour to 24 hour (my bad!)

Date d = null;
String str1 = "2011-08-15 11:36:16 PM";
try {
    d = new SimpleDateFormat("yyyy-MM-dd KK:mm:ss a").parse(str1);
}
catch (ParseException e) {
    e.printStackTrace();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(d));


这篇关于将12小时格式转换为24小时格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆