Java如何动态读取日期

标签 java date

我有一个包含六个数字的字符串:650310。它以 YYMMDD 格式表示 1965 March 10

是否有任何方法可以将这种格式识别为1965年3月10日? 目前这是我的做法,效果不是很好。

public class Example {

    public static void main(String args[]) {
        //date in YYMMDD

        //String x = "650310";
        String x = "161020";
        System.out.print(x.substring(4, 6)+" ");

        if (Integer.parseInt(x.substring(2, 4)) == 10) {
            System.out.print("October"+" ");
        }
        else  if (Integer.parseInt(x.substring(2, 4)) == 03) {
            System.out.print("March"+" ");
        }
        if (Integer.parseInt(x.substring(0, 2)) > 50) {
            String yr = "19" + x.substring(0, 2);
            System.out.println(yr);
        } else if (Integer.parseInt(x.substring(0, 2)) < 50) {
            String yr = "20" + x.substring(0, 2);
            System.out.println(yr);
        }
    }
}

output : 20 October 2016

最佳答案

使用Java的SimpleDateFormat:

SimpleDateFormat inFormat = new SimpleDateFormat( "yyMMdd" );
Date theDate = format.parse( "650310" );

现在您有了一个 Date 对象,您可以使用它以其他格式显示日期:

SimpleDateFormat outFormat = new SimpleDateFormat( "dd MMMMM yyyy" );
StringBuffer output = outFormat.format( theDate );

使用output.toString()显示新格式化的日期。祝你好运。

关于Java如何动态读取日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19784335/

相关文章:

java - 如何跟踪 JTextField 中的任何更改以便立即更新其他文本字段?

angular - 使用 Typescript + Angular 填充日期输入

php - Zend 和 Doctrine 的日期格式

php - 使用 Doctrine2 从字符串中插入日期

java - Thread 上的 eetop 字段是做什么用的?

c# - 使用 XML-RPC 编写以 C#、Ruby 和 Java 编写的分布式应用程序

java - UserDetailsS​​ervice中注入(inject)的HttpServletRequest不是过滤器添加的包装的HttpServletRequest吗?

java - JPanel 中的多个动画(线程)

python - 按日期在文本文件中搜索

delphi - 在 TDateTimePicker On 下拉菜单中设置日期