30

我正在使用以下格式在 Android 中显示日期和时间:
2013-06-18 12:41:24

如何将其更改为以下格式?
2013 年 6 月 18 日下午 12:41

4

11 回答 11

101

这是工作代码

public String parseDateToddMMyyyy(String time) {
    String inputPattern = "yyyy-MM-dd HH:mm:ss";
    String outputPattern = "dd-MMM-yyyy h:mm a";
    SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
    SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);

    Date date = null;
    String str = null;

    try {
        date = inputFormat.parse(time);
        str = outputFormat.format(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return str;
}

文档:SimpleDateFormat | 安卓开发者

于 2013-06-25T07:39:53.447 回答
6
    SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy  hh:mm a");
    String date = format.format(Date.parse("Your date string"));
于 2013-06-25T06:54:38.547 回答
5
public class MainActivity extends AppCompatActivity {
    private Date oneWayTripDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String date ="2017-05-05 13:58:50 ";
        SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat output = new SimpleDateFormat("MMMM dd,yyyy @hh:mm:ss aa");
        try {
            oneWayTripDate = input.parse(date);  // parse input
        } catch (ParseException e) {
            e.printStackTrace();
       }
       Log.e("===============","======currentData======"+output.format(oneWayTripDate);
    }
}
于 2017-05-02T07:08:48.867 回答
4

使用如下代码:

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy hh:mm a");
String date = formatter.format(Date.parse("Your date string"));

希望它会帮助你。

于 2013-06-25T06:55:01.800 回答
4

我在我的项目中修改了 Rusabh 的通用日期格式的答案

    public String formatDate(String dateToFormat, String inputFormat, String outputFormat) {
        try {
            Logger.e("DATE", "Input Date Date is " + dateToFormat);

            String convertedDate = new SimpleDateFormat(outputFormat)
                    .format(new SimpleDateFormat(inputFormat)
                            .parse(dateToFormat));

            Logger.e("DATE", "Output Date is " + convertedDate);

            //Update Date
            return convertedDate;

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;

    }

用法:

            String inputFormat = "yyyy-MM-dd'T'HH:mmz";
            String OutPutFormat = "MMMM dd', 'yyyy hh:mma";
            String convertedDate = formatDate(asOfDateTime, inputFormat, OutPutFormat);
于 2018-05-28T08:19:06.373 回答
2

你必须这样设置

 Date date=new Date("Your date "); 
SimpleDateFormat formatter5=new SimpleDateFormat("required format");
String formats1 = formatter5.format(date);
System.out.println(formats1);
于 2020-07-09T12:51:54.580 回答
2

像这样设置答案来设置日期格式

 Date date=new Date("Your date "); 
SimpleDateFormat formatter5=new SimpleDateFormat("required format");
于 2020-07-13T06:50:45.773 回答
1

使用下面的代码:

Date date=new Date("2013-06-18 12:41:24"); 
SimpleDateFormat formatter5=new SimpleDateFormat("dd-MM-yyyy hh:mm a");
String formats1 = formatter5.format(date);
System.out.println(formats1);
于 2013-06-25T06:56:10.210 回答
0

Calendar首先使用您的对象创建一个Date对象。然后构建String您需要的使用日期、年份、月份等。那么你就可以使用它了。

您可以在类中使用get()方法获取数据。Calendar

于 2013-06-25T07:08:01.653 回答
0

我们可以使用这种格式来转换日期。将日期作为参数传递

  public String formatdate(String fdate)
{
    String datetime=null;
    DateFormat inputFormat = new SimpleDateFormat("dd-MM-yyyy");
    SimpleDateFormat d= new SimpleDateFormat("yyyy-MM-dd");
    try {
        Date convertedDate = inputFormat.parse(fdate);
        datetime = d.format(convertedDate);

    }catch (ParseException e)
    {

    }
    return  datetime;


}
于 2016-11-28T11:15:03.393 回答
0
private val dateFormatter: Format = SimpleDateFormat(
    android.text.format.DateFormat.getBestDateTimePattern(
        Locale.getDefault(),
        "dMMyyjjmmss"
    ),
    Locale.getDefault()
)

val dateText = dateFormatter.format(Date(System.currentTimeMillis()))

这是 Android 上可用的最佳选择,它将根据设备默认定位格式化时间

取决于区域设置的示例输出:

  • 美国 - 02/18/2021, 1:00:00 PM(美国使用 12 格式时间)
  • 乌克兰 - 18.2.2012, 13:00:00(乌克兰使用 24 格式时间)

所以它会自动为您完成所有艰巨的工作

PS 替换yyyyyy如果您需要 2021 而不是 21,MM如果MMM您想将月份显示为单词而不是数字等等

例如"dMMMyyyyjjmmss"对于美国将返回:

Feb 18, 2021, 1:43:20 PM

对于俄罗斯:

18 февр. 2021 г., 13:44:18

很神奇,不是吗?

于 2021-02-18T11:33:34.347 回答