
您提供的用于 解析 的格式字符串与您实际获得的文本格式不匹配。您需要先解析,然后再格式化。看起来像您想要的:
SimpleDateFormat inputFormat = new SimpleDateFormat( "EEE MMM dd HH:mm:ss 'GMT' yyyy", Locale.US);inputFormat.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));SimpleDateFormat outputFormat = new SimpleDateFormat("MMM dd, yyyy h:mm a");// Adjust locale and zone appropriatelyDate date = inputFormat.parse(inputText);String outputText = outputFormat.format(date);编辑:这是简短但完整的程序形式的相同代码,并带有您的示例输入:
import java.util.*;import java.text.*;public class Test { public static void main(String[] args) throws ParseException { String inputText = "Tue May 21 14:32:00 GMT 2012"; SimpleDateFormat inputFormat = new SimpleDateFormat ("EEE MMM dd HH:mm:ss 'GMT' yyyy", Locale.US); inputFormat.setTimeZone(TimeZone.getTimeZone("Etc/UTC")); SimpleDateFormat outputFormat = new SimpleDateFormat("MMM dd, yyyy h:mm a"); // Adjust locale and zone appropriately Date date = inputFormat.parse(inputText); String outputText = outputFormat.format(date); System.out.println(outputText); }}您可以编译并运行 该确切代码 吗?
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)