getQauntdate() method explanation

huangapple go评论69阅读模式
英文:

getQauntdate() method explanation

问题

这是一个在脚本中找到的方法,你想知道它的功能。这个方法的功能是根据给定的数量(quant)获取日期。它使用了一个 SimpleDateFormat 对象来定义日期的格式为 "MMdd"。然后,它将给定的数量作为一年中的第几天(dayOfYear)设置到一个 Calendar 对象中。接着,它获取 Calendar 对象的时间(dat),并将其格式化为指定格式的字符串,最后返回这个字符串。

你想将这个方法翻译成 Golang,但是你不理解它的功能。

英文:

can someone explain to me the function of this methode that i found in a script:

public static String getQuantDate(final int quant) {
        final SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
        final int dayOfYear = quant;
        final Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, dayOfYear);
        final Date dat = calendar.getTime();
        return sdf.format(dat);
    }

i need to traslate it to golang but i didn't understand the functionalities to translate it!

答案1

得分: 0

注释:

// 格式化字符串。这将返回MMdd
final SimpleDateFormat sdf = new SimpleDateFormat("MMdd");

// 重复声明函数参数
final int dayOfYear = quant;

// 创建一个日期并将DAY_OF_YEAR设置为quant
final Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, dayOfYear);

// 获取日期并以正确的格式返回
final Date dat = calendar.getTime();
return sdf.format(dat);

看起来这个函数接受一个数字,将其转换为日期,并进行格式化。

1 将返回 0101

13 将返回 0113

32 将返回 0201

依此类推。

不过,这段代码不清楚如何处理闰年和其他变量。它似乎不是很高质量的代码,我建议您分析一下您的问题,并制定一个好的规范。

英文:

Annotated:

   // format string. This returns MMdd
    final SimpleDateFormat sdf = new SimpleDateFormat("MMdd");

    //redundant re-declaration of function parameter
    final int dayOfYear = quant;

    // make a date and set DAY_OF_YEAR to quant
    final Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.DAY_OF_YEAR, dayOfYear);

    // get date and return it in the correct format
    final Date dat = calendar.getTime();
    return sdf.format(dat);

Looks like the funtion takes a number, converts it to a date, and formats it.

1 would yield 0101

13 would give 0113

32 would give 0201

and so forth.

It is not clear though, how this handles leap years and other variables like that. It does not seem like very high quality code, and I'd recommend analyzing your problem, and coming up with a good spec.

huangapple
  • 本文由 发表于 2017年8月1日 21:32:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/45439343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定