【问题标题】:How to reset/update a counter based on a particular month?如何根据特定月份重置/更新计数器?
【发布时间】:2015-04-02 00:03:44
【问题描述】:

我正在努力想出一个干净的方法来做到这一点,所以任何帮助都会很棒。

需要重新设置标志,例如,从用户指定的日期开始的每年四月(用户输入)。那就是:
2015 年 1 月 1 日 --> 0
2015 年 2 月 1 日 --> 0
2015 年 3 月 1 日 --> 0
2015 年 4 月 1 日 --> 1
2015 年 5 月 1 日 --> 1
.
.
.
2016 年 1 月 1 日 --> 1
.
.
2016 年 4 月 1 日 --> 2
2016 年 5 月 1 日 --> 2
2016 年 6 月 1 日 --> 2
.
.
.
2017 年 1 月 1 日 --> 2
.
.
2017 年 4 月 1 日 --> 3
.
.

我正在尝试在 Apex 中执行此操作,但任何流行语言的算法就足够了。谢谢!

【问题讨论】:

  • 存储有问题的日期,计算到当前日期的年偏移量?! : 04/01/2014 被选中 -> 04/01/2017 - 04/01/2014 = 3

标签: java algorithm functional-programming apex


【解决方案1】:

您可以使用CalendarSimpleDateFormat 并执行以下操作:

import java.text.SimpleDateFormat;
import java.util.Calendar;

public static void main(String[] args) {

  int counter = 0;
  SimpleDateFormat test = new SimpleDateFormat("04/25/2015");
  Calendar cal = Calendar.getInstance();
  if (test.MONTH_FIELD == Calendar.APRIL);
  {
      counter++;
  }
}

【讨论】:

    猜你喜欢
    • 2019-05-15
    • 2017-01-01
    • 1970-01-01
    • 2012-04-15
    • 2015-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多