Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将日期字符串转换为特定的日期格式;“年月日”;在爪哇_Java_Android_Datetime_Date Format - Fatal编程技术网

Java 将日期字符串转换为特定的日期格式;“年月日”;在爪哇

Java 将日期字符串转换为特定的日期格式;“年月日”;在爪哇,java,android,datetime,date-format,Java,Android,Datetime,Date Format,我有多个有出生日期的联系人,所有联系人都与移动设备同步。现在的问题是,所有联系人都有不同的生日格式,我想以“dd-MM-yyyy”这样的特定格式显示所有的生日日期 例如,一个同步联系人的生日是“1990-02-07”或“1988-06-15T22:00:00.000Z”或“12-02-1990”等。。。 然后,所有这些日期都应以特定格式“dd-MM-yyyy”显示 那么我如何解决这个问题呢 任何建议都将不胜感激。只需使用SimpleDataFormat类即可。比如: Date d = Calen

我有多个有出生日期的联系人,所有联系人都与移动设备同步。现在的问题是,所有联系人都有不同的生日格式,我想以“dd-MM-yyyy”这样的特定格式显示所有的生日日期

例如,一个同步联系人的生日是“1990-02-07”或“1988-06-15T22:00:00.000Z”或“12-02-1990”等。。。 然后,所有这些日期都应以特定格式“dd-MM-yyyy”显示

那么我如何解决这个问题呢


任何建议都将不胜感激。

只需使用SimpleDataFormat类即可。比如:

Date d = Calendar.getInstance().getTime(); // Current time
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); // Set your date format
String currentData = sdf.format(d); // Get Date String according to date format
您可以在此处查看详细信息和所有支持的格式:


我知道你的问题出在这里。您可以尝试用“-”分隔字符串,并将其存储在数组中,然后检查数组中的每个字符串,将其解析为int,然后执行条件语句

假设您提供的具有以下格式 “1990-02-07”年-月-日 “1988-06-15T22:00:00.000Z”年-月-日 “12-02-1990”月日年

你可以试试这样的

    public String convert(String s){
        SimpleDateFormat newformat = new SimpleDateFormat("dd-MM-yyyy");
        try{
            if(s.contains("T")){
                String datestring = s.split("T")[0];
                SimpleDateFormat oldformat = new SimpleDateFormat("yyyy-MM-dd");
                String reformattedStr = newformat.format(oldformat.parse(datestring));
                return reformattedStr;
            }
            else{
                if(Integer.parseInt(s.split("-")[0])>13){
                    SimpleDateFormat oldformat = new SimpleDateFormat("yyyy-MM-dd");
                    String reformattedStr = newformat.format(oldformat.parse(s));
                    return reformattedStr;
                }
                else{
                    SimpleDateFormat oldformat = new SimpleDateFormat("MM-dd-yyyy");
                    String reformattedStr = newformat.format(oldformat.parse(s));
                    return reformattedStr;
                }

            }
        }
        catch (Exception e){
            return null;
        }
    }
ISO 8601 现在的问题是所有的联系人都有不同的生日格式

这才是真正的问题:以各种格式存储日期时间值

将日期时间值序列化为文本时,始终使用标准格式。这些格式既合理又实用,避免了歧义,易于机器解析,也易于跨文化的人类阅读

这些类在解析/生成字符串时默认使用这些标准格式

java.time 现代方法使用java.time类来取代麻烦的旧遗留日期时间类。避免使用传统的
日期
日历
简化格式
,以及相关的类

例如,一个同步联系人的生日是“1990-02-07”或“1988-06-15T22:00:00.000Z”或“12-02-1990”等

由于模糊性,无法解析任何可能的日期时间格式。例如,
01-02-1990
代表一月二日还是二月一日

如果您愿意,您可以猜测,尽管这可能是不明智的,这取决于准确性对您的业务问题的重要性

使用定义一组格式化模式。每种都试试。抛出后,继续下一个模式,直到其中一个模式起作用

您可以使用来帮助指导猜测

List < DateTimeFormatter > dateFormatters = new ArrayList <>( 2 );
dateFormatters.add( DateTimeFormatter.ofPattern( "uuuu-MM-dd" ) );  // BEWARE of ambiguity in these formatters regarding month-versus-day.
dateFormatters.add( DateTimeFormatter.ofPattern( "dd-MM-uuuu" ) );

String input = "1990-02-07";
// String input = "12-02-1990" ;

if ( null == input )
{
    throw new IllegalArgumentException( "Passed null argument where a date-time string is expected. Message # c7a4fe0e-9500-45d5-a041-74d457381008." );
} else if ( input.length() <= 10 )
{
    LocalDate ld = null;
    for ( DateTimeFormatter f : dateFormatters )
    {
        try
        {
            ld = LocalDate.parse( input , f );
            System.out.println( ld );
        } catch ( Exception e )
        {
            // No code here.
            // We purposely ignore this exception, moving on to try the next formatter in our list.
        }
    }
} else if ( ( input.length() > 10 ) && input.substring( input.length() - 1 ).equalsIgnoreCase( "Z" ) ) // If over 10 in length AND ends in a Z.
{
    Instant ld = null;
    try
    {
        ld = Instant.parse( input );  // Uses `DateTimeFormatter.ISO_INSTANT` formatter.
    } catch ( Exception e )
    {
        throw new IllegalArgumentException( "Unable to parse date-time string argument. Message # 0d10425f-42f3-4e58-9baa-84ff949e9574." );
    }
} else if ( input.length() > 10 )
{
    // TODO: Define another list of formatters to try here.
} else if ( input.length() == 0 )
{
    throw new IllegalArgumentException( "Passed empty string where a date-time string is expected. Message # 0ffbd9b6-8905-4e28-a732-0f402d4673df." );
} else  // Impossible-to-reach, for defensive programming.
{
    throw new RuntimeException( "ERROR - Unexpectedly reached IF-ELSE when checking input argument. Message # 6228d9e0-047a-4b83-8916-bc526e0fd22d." );
}
System.out.println("Done running.");
ListdateFormatters=newarraylist(2);
dateFormatters.add(模式的DateTimeFormatter.of(“uuu-MM-dd”);//注意这些格式化程序中关于月与日的模糊性。
添加(模式的DateTimeFormatter.of(“dd-MM-uuu”);
字符串输入=“1990-02-07”;
//字符串输入=“12-02-1990”;
if(null==输入)
{
抛出新的IllegalArgumentException(“在需要日期时间字符串的位置传递空参数。消息#c7a4fe0e-9500-45d5-a041-74d457381008”);
}else if(input.length()10)和&input.substring(input.length()-1.equalsIgnoreCase(“Z”)//如果长度超过10并以Z结尾。
{
瞬时ld=null;
尝试
{
ld=Instant.parse(input);//使用'DateTimeFormatter.ISO_Instant'格式化程序。
}捕获(例外e)
{
抛出新的IllegalArgumentException(“无法分析日期时间字符串参数。消息#0d10425f-42f3-4e58-9baa-84ff949e9574”);
}
}else if(input.length()>10)
{
//TODO:定义另一个要在此处尝试的格式化程序列表。
}else if(input.length()==0)
{
抛出新的IllegalArgumentException(“在需要日期时间字符串的位置传递空字符串。消息#0ffbd9b6-8905-4e28-a732-0f402d4673df”);
}否则//不可能到达,用于防御性编程。
{
抛出新的运行时异常(“错误-检查输入参数时意外到达IF-ELSE。消息#6228d9e0-047a-4b83-8916-bc526e0fd22d”);
}
System.out.println(“完成运行”);
1990-02-07

跑完了


关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,&

该项目现已启动,建议迁移到类

要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是

您可以直接与数据库交换java.time对象。使用兼容的或更高版本。不需要字符串,也不需要
java.sql.*

从哪里获得java.time类

  • ,及以后
    • 内置的
    • 标准JavaAPI的一部分,带有捆绑实现
    • Java9添加了一些次要功能和修复
    • 大部分java.time功能都在中向后移植到Java6和Java7
    • 更高版本的Android捆绑包实现了java.time类

    • 对于早期的Android(尝试搜索Java日期格式,在这个问题上已经有数百个类似的问题…如果我知道原始日期字符串格式,我可以将给定字符串转换为以下特定格式的日期。但是,如果我不知道原始字符串格式,我该如何做。如果指定字符串格式,SimpleDateFormat类应该完全缺少带有“零”的日期部分。请进行校对,像、
      java.text.SimpleDateFormat
      这样麻烦的旧日期-时间类现在被内置在java 8和java 9中的类所取代。请参阅。如果我知道其起源,我可以将给定字符串转换为特定格式的日期,如下所示l日期字符串格式。但是如果我不知道原始字符串格式,我怎么做。嗯,我想你必须知道同步的格式。检查这个我相信月总是在前一天。你这里有三个最终格式:月da