简体   繁体   English

如何将 YY/MM/DD 类型的字符串转换为 java.util.Date?

[英]How to convert string of type YY/MM/DD to java.util.Date?

I have searched but did not find the exact format(YY/MM/DD) for parsing the String.我已经搜索过,但没有找到解析字符串的确切格式(YY/MM/DD)。

How can I convert a string of type YY/MM/DD to java.util.Date .如何将 YY/MM/DD 类型的字符串转换为java.util.Date I have strings in the format "160310", "160211".我有格式为“160310”、“160211”的字符串。

You can use SimpleDateFormat for this.您可以为此使用SimpleDateFormat

String target = "160211";
DateFormat df = new SimpleDateFormat("yyMMdd", Locale.ENGLISH);
Date result =  df.parse(target);

For more options and info you can always checkout the full documentation about it here: http://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html有关更多选项和信息,您可以随时在此处查看有关它的完整文档: http : //docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html

Nit: The format isn't "YY/MM/DD", it's "YYMMDD" (note the slashes). Nit:格式不是“YY/MM/DD”,而是“YYMMDD”(注意斜线)。 Regardless, you can use a SimpleDateFormat to parse such strings:无论如何,您可以使用SimpleDateFormat来解析此类字符串:

DateFormat df = new SimpleDateFormat("yyMMdd");
Date date = df.parse("160211");

Use the following code , It will使用以下代码,它将

    String mydate="160310";
    SimpleDateFormat sd=new SimpleDateFormat("YYmmdd",Locale.ENGLISH);
    Date date = sd.parse(mydate);
    System.out.println(date);`

If you have reached the area of Java 8, you might also consider如果你已经到了 Java 8 的领域,你也可以考虑

        Date dt = Date.valueOf(LocalDate.from(DateTimeFormatter.ofPattern("yyMMdd").parse("160123")));

But in fact, you would not do the conversion to the old ugly date if you can avoid it, but rather go along with the LocalDate you created on your way.但实际上,如果可以避免的话,您不会转换为旧的丑陋日期,而是使用您在途中创建的 LocalDate。

Like this:像这样:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTester {

    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");

        Date date = null;
        try {
            date = sdf.parse("160310");
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(date);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以(yyyy-MM-dd)格式将String转换为java.util.Date,返回类型为Date - Convert String to java.util.Date in the (yyyy-MM-dd) format with return type of Date 如何将dd / MM / yyyy格式的java.sql.Date转换为java.util.Date? - How to convert java.sql.Date to java.util.Date in dd/MM/yyyy format? 无法使用@DateTimeFormat(pattern =“dd / MM / yyyy”)在java.util.Date中转换字符串 - Failed to convert string in java.util.Date with @DateTimeFormat(pattern=“dd/MM/yyyy”) 将java.util.Date转换为yyyy-MM-dd格式的String,而不创建大量对象 - Convert java.util.Date to String in yyyy-MM-dd format without creating a lot of objects 如何将 java.util.Date 转换为 soap 支持的日期格式“yyyy-MM-dd'T'HH:mm:ss”与区域 id - How to convert java.util.Date to soap suppported date format “yyyy-MM-dd'T'HH:mm:ss” with zone id 如何将 dd.mm.yy 类型的日期转换为 dd/mm/yy? - How to convert date of type dd.mm.yy to dd/mm/yy? 将YYYY-MM-DD HH:MM:SS格式的java.util.Date转换为与MySQL DATETIME兼容 - Convert java.util.Date in YYYY-MM-DD HH:MM:SS format to compatible with MySQL DATETIME 将格式为 yyyy-MM-dd 的字符串日期解析为 java.util.Date 格式为 yyyy-MM-dd - Parse String date of format yyyy-MM-dd to java.util.Date of format yyyy-MM-dd 如何在java中将模式dd-MMM-yy的字符串日期值转换为模式dd/MM/yyyy的日期对象 - how to convert a String date value of pattern dd-MMM-yy to Date object of Pattern dd/MM/yyyy in java 使用@DateTimeFormat(pattern =“ HH:mm”)无法将类型[java.lang.String]的属性值转换为所需的类型[java.util.Date] - Failed to convert property value of type [java.lang.String] to required type [java.util.Date] with @DateTimeFormat(pattern = “HH:mm”)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM