我如何简单地解析没有指定年份的日期? [英] How do I simply parse a date without a year specified?

查看:168
本文介绍了我如何简单地解析没有指定年份的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工具,它似乎给了我日期而没有指定我需要转换的年份,并且我正在使用Java进行任务(实际上Groovy,但在这种情况下足够接近)。示例日期是Dec Dec 12:00:00,应该参考2011年12月13日,因为年份没有指定,并且是2011年。以下Groovy脚本可以实现这一点:

  import java.text。* 
println new SimpleDateFormat(dd MMM HH:mm:ss)。parse(Dec Dec 12:00:00 )

这个脚本的问题在于,SimpleDateFormat似乎在转换后的1970年没有设置。我可以明确地将它设置为2011年,因为这是今年,但是在今年和所设定的日期之间似乎存在一些滞后,所以当新年来临时,这个剧本会在这段时间内错误。我怎样才能解决它?一个解决方案是检查本年的日期是否在现在之后,然后使用去年,但我希望有一个更简单的解决方案存在(或说明这是最简单的方法)。

java.util.Calendar 获取当前年份,如下例所示。

>

  Calendar.getInstance().get(Calendar.YEAR); 






示例代码片段



OP详细说明了他的问题,因此这段代码已被重写以符合他的需求。

  public static String fixDate(String data)throws Exception {
SimpleDateFormat dateInputFormat = new SimpleDateFormat(dd MMM HH:mm:ss);
SimpleDateFormat dateOutputFormat = new SimpleDateFormat(yyyy dd MMM HH:mm:ss);

日历currentCal = Calendar.getInstance();
日历parsedCal = Calendar.getInstance();

int CAL_YEAR = Calendar.YEAR;

parsedCal.setTime(dateInputFormat.parse(data));
parsedCal.set(CAL_YEAR,currentCal.get(CAL_YEAR)); $(b)b
if(parsedCal.after(currentCal))
parsedCal.set(CAL_YEAR,parsedCal.get(CAL_YEAR) - 1);

// Date parsedDate = parsedCal.getTime();

return dateOutputFormat.format(parsedCal.getTime());


public static void main(String [] args)throws Exception {
System.out.println(fixDate(13 Dec 12:00:00));
System.out.println(fixDate(31 Dec 12:00:00));
}

输出


2011年12月13日12:00:00

2010年12月31日12:00:00







记住要注意抛出异常

I've got a tool which seems to be giving me dates without specifying the year that I need to convert and I'm using Java for the task (actually Groovy but close enough in this case). An example date is "13 Dec 12:00:00", which should refer to 12/13/2011 since the year is unspecified and it is 2011. The following Groovy script does the trick:

import java.text.*
println new SimpleDateFormat("dd MMM HH:mm:ss").parse("13 Dec 12:00:00")

My problem with this script is that SimpleDateFormat seems to be leaving the year unset at 1970 after conversion. I could explicitly set it to 2011 because that is the current year but there seems to be some lag between current year and the dates set so when New Years comes this script will get it wrong for that lag time. How can I fix it up simply? One solution would be to check if the date with current year is after now then use the last year but I'm hoping a simpler solution exists (or state that's the simplest if not).

解决方案

You can use java.util.Calendar to get the current year, like in the below example.

Calendar.getInstance ().get (Calendar.YEAR);


Example snippet

OP elaborated his question and therefor this snippet has been rewritten to match his needs.

  public static String fixDate (String data) throws Exception {
    SimpleDateFormat dateInputFormat  = new SimpleDateFormat("dd MMM HH:mm:ss");
    SimpleDateFormat dateOutputFormat = new SimpleDateFormat("yyyy dd MMM HH:mm:ss");

    Calendar currentCal = Calendar.getInstance ();
    Calendar parsedCal  = Calendar.getInstance ();

    int CAL_YEAR  = Calendar.YEAR;

    parsedCal.setTime  (dateInputFormat.parse (data));
    parsedCal.set      (CAL_YEAR, currentCal.get (CAL_YEAR));

    if (parsedCal.after (currentCal))
      parsedCal.set (CAL_YEAR, parsedCal.get (CAL_YEAR) - 1);

    // Date parsedDate = parsedCal.getTime ();

    return dateOutputFormat.format (parsedCal.getTime ());
  }

  public static void main (String[] args) throws Exception {
    System.out.println (fixDate ("13 Dec 12:00:00"));
    System.out.println (fixDate ("31 Dec 12:00:00"));
  }

output

2011 13 Dec 12:00:00

2010 31 Dec 12:00:00


Remember to watch out for thrown Exceptions!

这篇关于我如何简单地解析没有指定年份的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆