java 日期格式转换安卓

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6896635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:48:17  来源:igfitidea点击:

Date format conversion Android

javaandroiddate

提问by Udaykiran

I have string form of date:

我有日期的字符串形式:

2011-03-27T09:39:01.607

and I want to format it to March 27, 2011

我想将其格式化为 March 27, 2011

I am using

我在用

DateFormat[] formats = new DateFormat[] {
DateFormat.getDateInstance(), DateFormat.getDateTimeInstance(),
         DateFormat.getTimeInstance(), };
String actDate= formats[0].format(uploadeddate.substring(0,9));

but its not working.

但它不工作。

How do I convert to March 27, 2011?

我如何转换为March 27, 2011

回答by Niranj Patel

try this

试试这个

SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
java.util.Date date = null;
try 
{
    date = form.parse("2011-03-27T09:39:01.607");
}
catch (ParseException e) 
{

    e.printStackTrace();
}
SimpleDateFormat postFormater = new SimpleDateFormat("MMMMM dd, yyyy");
String newDateStr = postFormater.format(date);

now newDateStr = March 27, 2011;

现在 newDateStr = March 27, 2011;

回答by Mudassir

May be this is of any help;

可能这有任何帮助;

String convertDate(String inputDate) {

    DateFormat theDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date date = null;

    try {
        date = theDateFormat.parse(inputDate);
    } catch (ParseException parseException) {
        // Date is invalid. Do what you want.
    } catch(Exception exception) {
        // Generic catch. Do what you want.
    }

    theDateFormat = new SimpleDateFormat("MMM dd, yyyy");

    return theDateFormat.format(date);
}

回答by Luis Ollero

You can use android.text.format.DateFormatin this way:

你可以这样使用android.text.format.DateFormat

DateFormat.getMediumDateFormat(context).format(date);

回答by PedroAGSantos

String dateimput = "2011-03-27T09:39:01.607"

SimpleDateFormat formatrecived = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");    
SimpleDateFormat formarwanted = new SimpleDateFormat("MMMM dd, yyyy");

Date recived = formatrecived.parse(dateimput);

Date output = formatwanted.format(recived);

Note: dateimput you need to format to yyyy-MM-dd hh:mm:ssusing replace and substring

注意:日期输入需要格式化为yyyy-MM-dd hh:mm:ss使用替换和子字符串

回答by John smith

To get AM or PM and 12 hour date format use hh:mm:ss aas string formatter where hhis for 12 hour format and ais for AM PM format.

要获取 AM 或 PM 和 12 小时日期格式,请hh:mm:ss a用作字符串格式化程序,其中hh12 小时格式和aAM PM 格式。

note ## HH is for 24 hour and hhis for 12 hour date format.

注意## HH 是 24 小时制,hh是 12 小时制日期格式。

SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
        String newFormat = formatter.format(testDate);

example

例子

String date = "2011/11/12 16:05:06";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/mm/dd HH:MM:SS");
    Date testDate = null;
    try {
        testDate = sdf.parse(date);
    }catch(Exception ex){
        ex.printStackTrace();
    }
    SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
    String newFormat = formatter.format(testDate);
    System.out.println(".....Date..."+newFormat);

回答by Ole V.V.

I should like to contribute the modern answer.

我想贡献现代答案。

java.time and ThreeTenABP

java.time 和 ThreeTenABP

    DateTimeFormatter dateFormatter
            = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);

    String uploadeddate = "2011-03-27T09:39:01.607";
    LocalDateTime ldt = LocalDateTime.parse(uploadeddate);
    String actDate = ldt.format(dateFormatter);
    System.out.println(actDate);

When I set my default locale to English and run this snippet, the output is what you asked for:

当我将默认语言环境设置为英语并运行此代码段时,输出就是您所要求的:

March 27, 2011

2011 年 3 月 27 日

I am exploiting the fact that your string is in the standard ISO 8601 format, and that the classes of java.time parse this format as their default. Usually when reformatting a date/time string from one format to another, two formatters are used, one that specifies the format to convert fromand one for the format to convert to. But since your source format is the default, we can make do with only one formatter here.

我正在利用这样一个事实,即您的字符串采用标准 ISO 8601 格式,并且 java.time 的类将此格式解析为它们的默认格式。通常从重新格式化为另一种格式的日期/时间字符串时,两名格式化使用,一个指定的格式转换,从一个用于格式转换。但是由于您的源格式是默认格式,因此我们只能在这里使用一种格式化程序。

What went wrong in your code?

你的代码出了什么问题?

  • Your counting is wrong: uploadeddate.substring(0,9)gives you 2011-03-2where I'm sure you intended 2011-03-27.
  • You passed a Stringto formats[0].format(), but it cannot format a string. It accepts either a Dateor a Long, so you would have needed to convert to one of these first. The code compiles, but throws java.lang.IllegalArgumentException: Cannot format given Object as a Date.
  • 你的计数是错误的:uploadeddate.substring(0,9)给你2011-03-2我确定你想要的地方2011-03-27
  • 您传递了一个Stringto formats[0].format(),但它无法格式化字符串。它接受 aDate或 a Long,因此您需要先转换为其中之一。代码编译,但抛出java.lang.IllegalArgumentException: Cannot format given Object as a Date.

Question: Can I use java.time on Android?

问题:我可以在 Android 上使用 java.time 吗?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

是的,java.time 在新旧 Android 设备上运行良好。它只需要至少Java 6

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It's called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bpwith subpackages.
  • 在 Java 8 及更高版本以及更新的 Android 设备(从 API 级别 26)中,现代 API 是内置的。
  • 在 Java 6 和 7 中获得 ThreeTen Backport,现代类的 backport(ThreeTen for JSR 310;请参阅底部的链接)。
  • 在(较旧的)Android 上使用 ThreeTen Backport 的 Android 版本。它被称为 ThreeTenABP。并确保从org.threeten.bp子包中导入日期和时间类。

Links

链接

回答by Basil Bourque

Example code using the Joda-Timelibrary.

使用Joda-Time库的示例代码。

DateTime dateTime = new DateTime( "2011-03-27T09:39:01.607", DateTimeZone.forID( "Asia/Kolkata" ) );
DateTimeFormatter formatter = DateTimeFormat.forStyle( "M-" );
String output = formatter.withLocale( java.util.Locale.ENGLISH ).print( dateTime );

回答by user3428518

public static String getFormatedDate(String strDate, String sourceFormate,
        String destinyFormate) {
    SimpleDateFormat df;
    df = new SimpleDateFormat(sourceFormate);
    Date date = null;
    try {
        date = df.parse(strDate);

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

    df = new SimpleDateFormat(destinyFormate);
    return df.format(date);

}

Call it like:

像这样称呼它:

getFormatedDate(strDate, "yyyy-MM-dd HH:mm:ss", "mm/dd/yyyy");

回答by Pengume

What you'll want to do is create a format for your date to be able to parse the date out into the way you want it.

您要做的是为您的日期创建一种格式,以便能够将日期解析为您想要的方式。

The formatted well have to be exactly the format of your timestamp and then what I did is create another format as to what I want the date to look like and then parse the it.

格式化的井必须与时间戳的格式完全一致,然后我所做的是创建另一种格式,以了解我希望日期的样子,然后解析它。

Check out the documentation for exactly what to use to format and then what to use to parse. Once I figured this out it got a lot easier.

查看文档以确切了解用于格式化的内容以及用于解析的内容。一旦我明白了这一点,事情就变得容易多了。