如何在java中将unix时间格式转换为日格式、月日期和时间格式

如何在java中将unix时间格式转换为日格式、月日期和时间格式,java,simpledateformat,Java,Simpledateformat,我的时间到了 SimpleDateFormat sdf=新的SimpleDateFormat(“yyyy-MM-dd'T'HH:MM:ss'Z'”) 输入日期我必须将其转换为其他时间格式 Mar 27,2018 5:18 pm 是否需要任何输出?请建议我如何在java中将给定时间转换为其他时间格式 SimpleDateFormat sdf = new SimpleDateFormat("MMM d,yyyy h:mm a"); System.out.println(sdf.format(dat

我的时间到了
SimpleDateFormat sdf=新的SimpleDateFormat(“yyyy-MM-dd'T'HH:MM:ss'Z'”)

输入日期我必须将其转换为其他时间格式

Mar 27,2018 5:18 pm
是否需要任何输出?请建议我如何在
java
中将给定时间转换为其他时间格式

SimpleDateFormat sdf = new SimpleDateFormat("MMM d,yyyy h:mm a");
System.out.println(sdf.format(date));
//2018年3月27日下午6:28

更多

您可以将任何类型转换为任何日期格式

您只需将字符串格式传递给
SimpleDateFormate

将此方法用作
static
,并通过传递
inputDate

简化日期转换方法:

public static String getFormattedDate(String inputDate)
{
    String outputFormattedDate = "";
    try
    {
        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z");
        Date inputFormatDate = inputFormat.parse(inputDate);

        SimpleDateFormat outputFormat = new SimpleDateFormat("MMM dd,yyyy h:mm a");
        outputFormattedDate = outputFormat.format(inputFormatDate);
    }
    catch (Exception ex)
    {
        outputFormattedDate = inputDate;
        ex.printStackTrace();
    }

    return outputFormattedDate;
}

希望它能帮助您。

如果您有Java 8,请使用Java.time API:

DateTimeFormatter parser = new DateTimeFormatterBuilder()
                .append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
                .appendPattern("XX")
                .toFormatter();
OffsetDateTime odt = OffsetDateTime.parse("2108-03-27T17:18:16.985+0530", parser);

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MMM dd,yyyy h:mm a", Locale.ENGLISH);
String formattedDate = fmt.format(odt); // Mar 27,2108 5:18 PM
SimpleDataFormat
已经解决了许多问题,其中许多问题是通过java.time API解决的,您应该更喜欢使用这些问题


对于较旧版本的Java,有一个相同的类和功能。

查看上的日期和时间模式,还可以查看日期和时间API您可以选择其他日期格式,而不是将给定的时间转换为其他时间格式。这里有其他时间格式的例子。试试这个:仅供参考,像、
java.text.SimpleDateFormat
这样麻烦的旧日期时间类现在被内置在Java8和Java9中的类所取代。请参阅。我的输入是2108-03-27T17:18:16.985+0530,使用此格式。我将如何将其转换为日期格式@Roushan45
public static String getFormattedDate(String inputDate)
{
    String outputFormattedDate = "";
    try
    {
        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z");
        Date inputFormatDate = inputFormat.parse(inputDate);

        SimpleDateFormat outputFormat = new SimpleDateFormat("MMM dd,yyyy h:mm a");
        outputFormattedDate = outputFormat.format(inputFormatDate);
    }
    catch (Exception ex)
    {
        outputFormattedDate = inputDate;
        ex.printStackTrace();
    }

    return outputFormattedDate;
}
DateTimeFormatter parser = new DateTimeFormatterBuilder()
                .append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
                .appendPattern("XX")
                .toFormatter();
OffsetDateTime odt = OffsetDateTime.parse("2108-03-27T17:18:16.985+0530", parser);

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MMM dd,yyyy h:mm a", Locale.ENGLISH);
String formattedDate = fmt.format(odt); // Mar 27,2108 5:18 PM