简体   繁体   English

如何解析“dd-MM”日期格式以获得当前年份?“

[英]How to parse “dd-MM” date format to get current year?"

I have to parse " 17-Jun " format date using Java.But the problem is when I try to parse " dd-MM " format using SimpleDateFormat it is returning as "Wed Jun 17 00:00:00 IST 1970" .Is it possible to get current(2014) year instead of 1970. 我必须使用Java解析“ 17-Jun ”格式的日期。但问题是,当我尝试使用SimpleDateFormat解析“ dd-MM ”格式时,它将返回“Wed Jun 17 00:00:00 IST 1970” 。是的有可能获得当前(2014)年而不是1970年。

My result: 我的结果:
17/JUNE/1970 17 / JUNE / 1970

Expected result: 预期结果:
17/JUNE/2014 17 / JUNE / 2014

Have a look at this.. 看看这个......

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);

To get year you can just use 为了获得一年,你可以使用

Calendar cal = Calendar.getInstance();
cal.get(Calendar.YEAR) will fetch you current year

Hope it helped... :) 希望它有所帮助...... :)

Try this 尝试这个

    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);

这样做

Date date = new SimpleDateFormat("dd-MMM-yyyy").parse("17-Jun-"+ Calendar.getInstance().get(Calendar.YEAR));

You'll have to write a utility method, there isn't anything in SimpleDateFormat that will interpret a non-existant year as the current year. 你必须编写一个实用程序方法,SimpleDateFormat中没有任何东西可以将不存在的年份解释为当前年份。 Something like this: 像这样的东西:

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();

}

Try this: 尝试这个:

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 );

your problem will be solved. 你的问题将得到解决。

I guess the simplest way is to do this: 我想最简单的方法是这样做:

DateFormat dateFormat = new SimpleDateFormat("yyyy/MMM/dd");
Date date = new Date();
System.out.println("Time is: " + dateFormat.format(date) );

This gives you exactly what you want. 这给了你你想要的。 also see 也看到了

http://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html http://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html

Little late, but if you really don't want to use Calendar at all - as I gather from your comments to the correct answers above - (not recommended with the usage of deprecated methods, but still): 迟到了,但如果你真的根本不想使用日历 - 我从你的评论中收集到上面的正确答案 - (不推荐使用不推荐的方法,但仍然):

SimpleDateFormat format = new SimpleDateFormat("dd-MMM");
Date date = format.parse("17-JUN");
date.setYear(new Date().getYear());
System.out.println(date);

Output: 输出:

Tue Jun 17 00:00:00 IST 2014 Tue Jun 17 00:00:00 IST 2014

All answers given here are more or less correct, but I notice that one detail aspect is still overlooked, namely if the combination of day and months fits to current year (february 29 problem). 这里给出的所有答案或多或少都是正确的,但我注意到一个细节方面仍然被忽视,即如果日和月的组合适合当前年份(2月29日的问题)。 So I would suggest a strict parsing like following: 所以我建议严格的解析如下:

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));

java.time java.time

In Java 8 you can do something like: 在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());

Try, 尝试,

    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();
    }

Output 产量

Before Changing :: 17/Jun/1970
After Changing  :: 17/Jun/2014

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM