Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.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 - Fatal编程技术网

Java 将日期转换为特定格式

Java 将日期转换为特定格式,java,android,Java,Android,将此日期格式2013-01-15 06:20:00转换为此格式2013-01-15。只需从日期中删除时间即可。我曾经 SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-mm-dd"); //Date convertedDate = new Date(); Date convertedDate = null; try {

将此日期格式2013-01-15 06:20:00转换为此格式2013-01-15。只需从日期中删除时间即可。我曾经

SimpleDateFormat dateFormat = new SimpleDateFormat(
                    "yyyy-mm-dd");
            //Date convertedDate = new Date();
            Date convertedDate = null;
            try {
                 convertedDate = dateFormat.parse(dateString);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

如何从日期获取或删除时间。

尝试使用以下格式:

SimpleDataFormat dateFormat=新SimpleDataFormatyyyy MM dd

然后解析一个新的日期:

字符串s=dateFormat.formatnew日期

只用

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date convertedDate = null;
            try {
                 convertedDate = dateFormat.parse(dateString);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
使用

输出
1991年2月20日

如果您只想分析它以用于显示,您可以使用以下方法:

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

public class DateFormatting {
    public static void main(String[] args) throws ParseException {
        String input = "2013-01-15 06:20:00";
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date theDate = dateFormat.parse(input);
        String output = dateFormat.format(theDate);
        System.out.println(output); // prints 2013-01-15
    }
}
String input = "2013-01-15 06:20:00";
System.out.println(input.split(" ")[0]);

我也使用了这种格式,但是它的生成方式是这样的:Sun Jul 06 00:00:00 IST 1975日期仍然有一个时间分量,你不能去掉它。格式只是决定输出的外观,因为在这些情况下,这通常是最重要的。如果你得到了上面的输出,你就不是在使用我提到的格式。那么这个问题还有其他解决方案吗?回答得好,效率也很高。就两行。太好了。这发出了错误的日期格式。再试一次。
String input = "2013-01-15 06:20:00";
System.out.println(input.split(" ")[0]);