java中的日期格式

java中的日期格式,java,date,format,Java,Date,Format,我的日期格式是2011年8月4日,我想要20110804。那么我该怎么做呢?使用以下格式: SimpleDateFormat sdf = new SimpleDateFormat("ddMMM", Locale.ENGLISH); try { sdf.parse(sDate); } catch (ParseException e) { // TODO Auto-generated catch block e.

我的日期格式是2011年8月4日,我想要20110804。那么我该怎么做呢?

使用以下格式:

      SimpleDateFormat sdf = new SimpleDateFormat("ddMMM", Locale.ENGLISH);

      try {

        sdf.parse(sDate);
      } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
我认为您需要区分解析和输出:

 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);

将日期格式更改为

 SimpleDateFormat parseFormat = new SimpleDateFormat("MMMdd", Locale.ENGLISH);
 SimpleDateFormat outputFormat = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
 String dateStr = "06Sep";
 // parse with 06Sep format
 Date din = parseFormat.parse(dateStr);
 // output with 20101106 format
 System.out.println(String.format("Output: %s", outputFormat.format(din)));

这是一个完整的工作样本。我希望下一次,你会做你自己的工作

 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);

-1:无法工作,因为“mm”将输出分钟而不是月份值。抱歉,我以为我按下了shift键,现在已编辑。可能是重复的您以前问过这个问题。是的,但我尝试过,但最近(2017年1月1日)出现了新错误我建议您放弃过时的
SimpleDataFormat
和friends,使用
java.time
包中的类(从2014年开始)。关于如何,请看。但我犯了这个错误。。无法解析的日期:“06Sep”@Praneel,这不是你发布的示例。阅读javadocs了解其他格式!您需要区分解析和输出。如果您的日期是ni“06Sep”格式,则需要新的
SimpleDateFormat(“ddMMM”)
/**
 * 
 */

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


/**
 * @author The Elite Gentleman
 *
 */
public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            String date = "06Sep2011";
            SimpleDateFormat sdf = new SimpleDateFormat("ddMMMyyyy", Locale.ENGLISH);
            Date d = sdf.parse(date);

            SimpleDateFormat nsdf = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
            String nd = nsdf.format(d);
            System.out.println(nd);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}