我需要使用Java解析日期格式为“17-Jun”,但问题是,当我尝试使用SimpleDateFormat解析格式为“dd-MM”的日期时,它返回的是"Wed Jun 17 00:00:00 IST 1970"。是否可能获取当前(2014)年份而不是1970年?
我的结果:
17/JUNE/1970
期望的结果:
17/JUNE/2014
我需要使用Java解析日期格式为“17-Jun”,但问题是,当我尝试使用SimpleDateFormat解析格式为“dd-MM”的日期时,它返回的是"Wed Jun 17 00:00:00 IST 1970"。是否可能获取当前(2014)年份而不是1970年?
我的结果:
17/JUNE/1970
期望的结果:
17/JUNE/2014
看一下这个...
Calendar c = Calendar.getInstance();
c.set(Calendar.DATE, 17);
c.set(Calendar.MONTH, 5);
c.set(Calendar.YEAR, c.get(Calendar.YEAR));
Date date=new Date(c.getTimeInMillis());
SimpleDateFormat simpleDateformatter = new SimpleDateFormat("dd/mmm/yyyy");
String convertedDate = simpleDateformatter .format(date);
Calendar cal = Calendar.getInstance();
cal.get(Calendar.YEAR) will fetch you current year
试试这个
Calendar c = Calendar.getInstance();
c.set(Calendar.DATE, 17);
c.set(Calendar.MONTH, 5);
c.set(Calendar.YEAR, c.get(Calendar.YEAR));
Date d=new Date(c.getTimeInMillis());
SimpleDateFormat formatter = new SimpleDateFormat("dd- mmm");
String conDate = formatter.format(d);
在Java 8中,您可以执行以下操作:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("d-MMM");
MonthDay md = MonthDay.parse("17-Jun", dtf);
LocalDate d = LocalDate.now().with(md);
System.out.println(d.getDayOfMonth());
System.out.println(d.getMonthValue());
System.out.println(d.getYear());
这样做
Date date = new SimpleDateFormat("dd-MMM-yyyy").parse("17-Jun-"+ Calendar.getInstance().get(Calendar.YEAR));
你需要编写一个实用方法,SimpleDateFormat中没有任何东西可以将不存在的年份解释为当前年份。可以尝试像这样编写:
public static Date parseDate(String dateString) throws ParseException {
//determine current year
Calendar today = Calendar.getInstance();
int currentYear = today.get(Calendar.YEAR);
//parse input
SimpleDateFormat format = new SimpleDateFormat("dd-MMM");
Date parsed = format.parse(dateString);
// set current year on parsed value
Calendar cal = Calendar.getInstance();
cal.setTime(parsed);
cal.set(Calendar.YEAR, currentYear);
return cal.getTime();
}
试试这个:
SimpleDateFormat dfDate = new SimpleDateFormat("dd-MM-yyyy");
java.util.Date d = null;
try {
d = dfDate.parse("17-Jun-"+ Calendar.getInstance().get(Calendar.YEAR));
} catch (java.text.ParseException e) {
e.printStackTrace();
}
System.out.println(""+d );
您的问题将会得到解决。
DateFormat dateFormat = new SimpleDateFormat("yyyy/MMM/dd");
Date date = new Date();
System.out.println("Time is: " + dateFormat.format(date) );
http://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html
虽然有点晚,但如果您真的不想使用日历 - 正如我从您对上面正确答案的评论中了解到的一样 - (不推荐使用已弃用的方法,但仍然可以):
SimpleDateFormat format = new SimpleDateFormat("dd-MMM");
Date date = format.parse("17-JUN");
date.setYear(new Date().getYear());
System.out.println(date);
输出:
2014年6月17日 星期二 00:00:00 IST
这里给出的所有答案都或多或少是正确的,但我注意到还有一个细节方面被忽略了,即日期和月份的组合是否适合当前年份(2月29日的问题)。因此,我建议进行严格的解析,如下所示:
String ddMMM = "17-Jun";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
sdf.setLenient(false); // in order to check for "29-Feb"
TimeZone tz = TimeZone.getDefault(); // or change to your specific time zone
Date date =
sdf.parse(ddMMM + "-" + new GregorianCalendar(tz).get(Calendar.YEAR));
试一试,
String s2 = "Wed Jun 17 00:00:00 1970";
SimpleDateFormat sdf1 = new SimpleDateFormat("E MMM dd hh:mm:ss yyyy");
SimpleDateFormat sdf2 = new SimpleDateFormat("dd/MMM/yyyy");
try {
Date d1 = sdf1.parse(s2);
System.out.println(d1);
String s3 = sdf2.format(d1);
System.out.println("Before Changing :: "+s3);
Calendar cal = Calendar.getInstance();
cal.setTime(d1);
cal.add(Calendar.YEAR, 2014-1970);
d1 = cal.getTime();
String s4 = sdf2.format(d1);
System.out.println("After Changing :: "+s4);
} catch (ParseException e) {
e.printStackTrace();
}
输出
Before Changing :: 17/Jun/1970
After Changing :: 17/Jun/2014