java java中的日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5220967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:02:35 来源:igfitidea点击:
Date format in java
提问by Praneel PIDIKITI
SimpleDateFormat sdf = new SimpleDateFormat("ddMMM", Locale.ENGLISH);
try {
sdf.parse(sDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
My date format is 04AUG2011 and i want to have 20110804. So how can i do that ?
我的日期格式是 04AUG2011,我想要 20110804。那么我该怎么做呢?
回答by Pablo Santa Cruz
Use the following format:
使用以下格式:
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
I think you need to differentiate between parsing and outputting:
我认为您需要区分解析和输出:
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)));
回答by Manoj
Change your dateformat to
将您的日期格式更改为
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
回答by Buhake Sindi
Here's a complete working sample. I hope next time, you will do your own work.
这是一个完整的工作示例。我希望下一次,你会做自己的工作。
/**
*
*/
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();
}
}
}