- 使用 Spring Initializr 创建 Spring Boot 应用程序
- 在Spring Boot中配置Cassandra
- 在 Spring Boot 上配置 Tomcat 连接池
- 将Camel消息路由到嵌入WildFly的Artemis上
本文整理了Java中java.time.YearMonth.until()
方法的一些代码示例,展示了YearMonth.until()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。YearMonth.until()
方法的具体详情如下:
包路径:java.time.YearMonth
类名称:YearMonth
方法名:until
[英]Calculates the period between this year-month and another year-month in terms of the specified unit.
This calculates the period between two year-months in terms of a single unit. The start and end points are this and the specified year-month. The result will be negative if the end is before the start. The Temporal passed to this method must be a YearMonth. For example, the period in years between two year-months can be calculated using startYearMonth.until(endYearMonth, YEARS).
The calculation returns a whole number, representing the number of complete units between the two year-months. For example, the period in decades between 2012-06 and 2032-05 will only be one decade as it is one month short of two decades.
This method operates in association with TemporalUnit#between. The result of this method is a long representing the amount of the specified unit. By contrast, the result of between is an object that can be used directly in addition/subtraction:
long period = start.until(end, YEARS); // this method
dateTime.plus(YEARS.between(start, end)); // use in plus/minus
The calculation is implemented in this method for ChronoUnit. The units MONTHS, YEARS, DECADES, CENTURIES, MILLENNIA and ERAS are supported. Other ChronoUnit values will throw an exception.
If the unit is not a ChronoUnit, then the result of this method is obtained by invoking TemporalUnit.between(Temporal, Temporal)passing this as the first argument and the input temporal as the second argument.
This instance is immutable and unaffected by this method call.
[中]根据指定的单位计算本年月份和另一年月份之间的期间。
这将根据单个单元计算两个月之间的时间。开始点和结束点为本月和指定的年-月。如果结束早于开始,结果将为负值。传递给此方法的时间必须为一年一个月。例如,可以使用startYearMonth计算两个年月之间的年周期。直到(年底月,年)。
计算结果返回一个整数,表示两年内的完整单元数。例如,从2012-06年到2032-05年的几十年期间将只有十年,因为距离二十年只差一个月。
该方法与TemporalUnit#between结合使用。此方法的结果是一个长的字符,表示指定单位的数量。相比之下,between的结果是一个可以直接用于加法/减法的对象:
long period = start.until(end, YEARS); // this method
dateTime.plus(YEARS.between(start, end)); // use in plus/minus
该计算是在ChronoUnit的这种方法中实现的。支持单位月、年、数十年、世纪、千年和年代。其他ChronoUnit值将引发异常。
如果该单元不是ChronoUnit,则通过调用TemporalUnit获得该方法的结果。(时态,时态)将其作为第一个参数传递,将输入时态作为第二个参数传递。
此实例是不可变的,不受此方法调用的影响。
代码示例来源:origin: stackoverflow.com
public static final long getMonthsDifference(Date date1, Date date2) {
YearMonth m1 = YearMonth.from(date1.toInstant());
YearMonth m2 = YearMonth.from(date2.toInstant());
return m1.until(m2, ChronoUnit.MONTHS) + 1;
}
代码示例来源:origin: owlike/genson
private static long getEpochMonth(YearMonth yearMonth){
return EPOCH_YEAR_MONTH.until(yearMonth, ChronoUnit.MONTHS);
}
代码示例来源:origin: stackoverflow.com
public static void main(String[] args) {
test("June, 1999", "April, 2002");
}
private static void test(String fromMonthYear, String toMonthYear) {
DateTimeFormatter monthYearFormat = DateTimeFormatter.ofPattern("MMMM, uuuu");
YearMonth from = YearMonth.parse(fromMonthYear, monthYearFormat);
YearMonth to = YearMonth.parse(toMonthYear , monthYearFormat);
long months = from.until(to, ChronoUnit.MONTHS);
System.out.printf("%d years and %d months%n", months / 12, months % 12);
}
代码示例来源:origin: OpenGamma/Strata
private double numberOfMonths(YearMonth month) {
return YearMonth.from(valuationDate).until(month, MONTHS);
}
代码示例来源:origin: OpenGamma/Strata
private DoubleArray buildNodeTimes(LocalDate valuationDate, CurveMetadata metadata) {
if (metadata.getXValueType().equals(ValueType.YEAR_FRACTION)) {
return DoubleArray.of(getParameterCount(), i -> {
LocalDate nodeDate = ((DatedParameterMetadata) metadata.getParameterMetadata().get().get(i)).getDate();
return getDayCount().get().yearFraction(valuationDate, nodeDate);
});
} else if (metadata.getXValueType().equals(ValueType.MONTHS)) {
return DoubleArray.of(getParameterCount(), i -> {
LocalDate nodeDate = ((DatedParameterMetadata) metadata.getParameterMetadata().get().get(i)).getDate();
return YearMonth.from(valuationDate).until(YearMonth.from(nodeDate), MONTHS);
});
} else {
throw new IllegalArgumentException("Metadata XValueType should be YearFraction or Months in curve definition");
}
}
代码示例来源:origin: OpenGamma/Strata
public void value_multiplicative() {
InflationNodalCurve curveComputed = InflationNodalCurve.of(CURVE_NOFIX, VAL_DATE_2, LAST_FIX_MONTH_2, LAST_FIX_VALUE,
SEASONALITY_MULTIPLICATIVE_DEF);
for (int i = 1; i < TEST_MONTHS.length; i++) {
double nbMonths = YearMonth.from(VAL_DATE_2).until(TEST_MONTHS[i], MONTHS);
double valueComputed = curveComputed.yValue(nbMonths);
int x = (int) ((nbMonths + 12) % 12);
double valueNoAdj = EXTENDED_CURVE_2.yValue(nbMonths);
double adj = SEASONALITY_MULTIPLICATIVE_COMP_2.get(x);
double valueExpected = valueNoAdj * adj;
assertEquals(valueExpected, valueComputed, TOLERANCE_VALUE);
}
}
代码示例来源:origin: OpenGamma/Strata
public void test_value() {
for (int i = 0; i < TEST_MONTHS.length; i++) {
double valueComputed = INSTANCE.value(TEST_OBS[i]);
YearMonth fixingMonth = TEST_OBS[i].getFixingMonth();
double valueExpected;
if (USCPI_TS.containsDate(fixingMonth.atEndOfMonth())) {
valueExpected = USCPI_TS.get(fixingMonth.atEndOfMonth()).getAsDouble();
} else {
double x = YearMonth.from(VAL_DATE).until(fixingMonth, MONTHS);
valueExpected = CURVE_INFL.yValue(x);
}
assertEquals(valueComputed, valueExpected, TOLERANCE_VALUE, "test " + i);
}
}
代码示例来源:origin: OpenGamma/Strata
public void value_additive() {
InflationNodalCurve curveComputed = InflationNodalCurve.of(CURVE_NOFIX, VAL_DATE_2, LAST_FIX_MONTH_2, LAST_FIX_VALUE,
SEASONALITY_ADDITIVE_DEF);
for (int i = 1; i < TEST_MONTHS.length; i++) {
double nbMonths = YearMonth.from(VAL_DATE_2).until(TEST_MONTHS[i], MONTHS);
double valueComputed = curveComputed.yValue(nbMonths);
int x = (int) ((nbMonths + 12) % 12);
double valueNoAdj = EXTENDED_CURVE_2.yValue(nbMonths);
DoubleArray seasonalityAdditiveCompounded =
seasonalityCompounded(VAL_DATE_2, LAST_FIX_MONTH_2, SEASONALITY_ADDITIVE, (v, a) -> v + a);
double adj = seasonalityAdditiveCompounded.get(x);
double valueExpected = valueNoAdj + adj;
assertEquals(valueExpected, valueComputed, TOLERANCE_VALUE);
}
}
代码示例来源:origin: OpenGamma/Strata
public void test_value_futfixing() {
for (int i = 0; i < TEST_MONTHS.length; i++) {
double valueComputed = INSTANCE_WITH_FUTFIXING.value(TEST_OBS[i]);
YearMonth fixingMonth = TEST_OBS[i].getFixingMonth();
double valueExpected;
if (fixingMonth.isBefore(YearMonth.from(VAL_DATE_2)) && USCPI_TS.containsDate(fixingMonth.atEndOfMonth())) {
valueExpected = USCPI_TS.get(fixingMonth.atEndOfMonth()).getAsDouble();
} else {
double x = YearMonth.from(VAL_DATE_2).until(fixingMonth, MONTHS);
valueExpected = CURVE_INFL2.yValue(x);
}
assertEquals(valueComputed, valueExpected, TOLERANCE_VALUE, "test " + i);
}
}
代码示例来源:origin: OpenGamma/Strata
private static DoubleArray seasonalityCompounded(
LocalDate valDate, YearMonth fixingMonth, DoubleArray seasonality,
DoubleBinaryOperator adjustmentFunction) {
double nbMonths = YearMonth.from(valDate).until(fixingMonth, MONTHS);
double[] seasonalityCompoundedArray = new double[12];
int lastMonthIndex = fixingMonth.getMonth().getValue() - 2;
seasonalityCompoundedArray[(int) ((nbMonths + 12 + 1) % 12)] =
seasonality.get((lastMonthIndex + 1) % 12);
for (int i = 1; i < 12; i++) {
int j = (int) ((nbMonths + 12 + 1 + i) % 12);
seasonalityCompoundedArray[j] = adjustmentFunction.applyAsDouble(
seasonalityCompoundedArray[(j - 1 + 12) % 12], seasonality.get((lastMonthIndex + 1 + i) % 12));
}
return DoubleArray.ofUnsafe(seasonalityCompoundedArray);
}
代码示例来源:origin: OpenGamma/Strata
public void test_value_parameter_sensitivity() {
for (int i = 0; i < TEST_MONTHS.length; i++) {
YearMonth fixingMonth = TEST_OBS[i].getFixingMonth();
if (!USCPI_TS.containsDate(fixingMonth.atEndOfMonth())) {
InflationRateSensitivity ptsExpected = (InflationRateSensitivity) InflationRateSensitivity.of(TEST_OBS[i], 1d);
CurrencyParameterSensitivities psComputed = INSTANCE.parameterSensitivity(ptsExpected);
double x = YearMonth.from(VAL_DATE).until(fixingMonth, MONTHS);
UnitParameterSensitivities sens1 = UnitParameterSensitivities.of(CURVE_INFL.yValueParameterSensitivity(x));
CurrencyParameterSensitivities psExpected =
sens1.multipliedBy(ptsExpected.getCurrency(), ptsExpected.getSensitivity());
assertTrue(psComputed.equalWithTolerance(psExpected, TOLERANCE_DELTA), "test " + i);
}
}
}
代码示例来源:origin: OpenGamma/Strata
public void parameter_sensitivity_additive() {
InflationNodalCurve curve = InflationNodalCurve.of(CURVE_NOFIX, VAL_DATE_2, LAST_FIX_MONTH_2, LAST_FIX_VALUE,
SEASONALITY_ADDITIVE_DEF);
double shift = 1.0E-2;
for (int i = 1; i < TEST_MONTHS.length; i++) {
double nbMonths = YearMonth.from(VAL_DATE_2).until(TEST_MONTHS[i], MONTHS);
UnitParameterSensitivity psComputed = curve.yValueParameterSensitivity(nbMonths);
for (int j = 0; j < TIMES.size(); j++) {
double[] valuePM = new double[2];
for (int pm = 0; pm < 2; pm++) {
DoubleArray shiftedValues = VALUES.with(j, VALUES.get(j) + (1 - 2 * pm) * shift);
InterpolatedNodalCurve intCurveShifted = InterpolatedNodalCurve.of(METADATA, TIMES, shiftedValues, INTERPOLATOR);
InflationNodalCurve seaCurveShifted =
InflationNodalCurve.of(intCurveShifted, VAL_DATE_2, LAST_FIX_MONTH_2, LAST_FIX_VALUE,
SEASONALITY_ADDITIVE_DEF);
valuePM[pm] = seaCurveShifted.yValue(nbMonths);
}
assertEquals(psComputed.getSensitivity().get(j), (valuePM[0] - valuePM[1]) / (2 * shift), TOLERANCE_DELTA);
}
}
}
代码示例来源:origin: OpenGamma/Strata
public void test_value_parameter_sensitivity_futfixing() {
for (int i = 0; i < TEST_MONTHS.length; i++) {
YearMonth fixingMonth = TEST_OBS[i].getFixingMonth();
if (!fixingMonth.isBefore(YearMonth.from(VAL_DATE_2)) && !USCPI_TS.containsDate(fixingMonth.atEndOfMonth())) {
InflationRateSensitivity ptsExpected = (InflationRateSensitivity) InflationRateSensitivity.of(TEST_OBS[i], 1d);
CurrencyParameterSensitivities psComputed = INSTANCE_WITH_FUTFIXING.parameterSensitivity(ptsExpected);
double x = YearMonth.from(VAL_DATE_2).until(fixingMonth, MONTHS);
UnitParameterSensitivities sens1 = UnitParameterSensitivities.of(CURVE_INFL2.yValueParameterSensitivity(x));
CurrencyParameterSensitivities psExpected =
sens1.multipliedBy(ptsExpected.getCurrency(), ptsExpected.getSensitivity());
assertTrue(psComputed.equalWithTolerance(psExpected, TOLERANCE_DELTA), "test " + i);
}
}
}
代码示例来源:origin: OpenGamma/Strata
public void parameter_sensitivity_multiplicative() {
InflationNodalCurve curve = InflationNodalCurve.of(CURVE_NOFIX, VAL_DATE_2, LAST_FIX_MONTH_2, LAST_FIX_VALUE,
SEASONALITY_MULTIPLICATIVE_DEF);
double shift = 1.0E-2;
for (int i = 1; i < TEST_MONTHS.length; i++) {
double nbMonths = YearMonth.from(VAL_DATE_2).until(TEST_MONTHS[i], MONTHS);
UnitParameterSensitivity psComputed = curve.yValueParameterSensitivity(nbMonths);
for (int j = 0; j < TIMES.size(); j++) {
double[] valuePM = new double[2];
for (int pm = 0; pm < 2; pm++) {
DoubleArray shiftedValues = VALUES.with(j, VALUES.get(j) + (1 - 2 * pm) * shift);
InterpolatedNodalCurve intCurveShifted = InterpolatedNodalCurve.of(METADATA, TIMES, shiftedValues, INTERPOLATOR);
InflationNodalCurve seaCurveShifted =
InflationNodalCurve.of(intCurveShifted, VAL_DATE_2, LAST_FIX_MONTH_2, LAST_FIX_VALUE,
SEASONALITY_MULTIPLICATIVE_DEF);
valuePM[pm] = seaCurveShifted.yValue(nbMonths);
}
assertEquals(psComputed.getSensitivity().get(j), (valuePM[0] - valuePM[1]) / (2 * shift), TOLERANCE_DELTA);
}
}
}
代码示例来源:origin: OpenGamma/Strata
double nbMonth = valuationMonth.until(lastMonth, MONTHS);
DoubleArray x = curveWithoutFixing.getXValues();
ArgChecker.isTrue(nbMonth < x.get(0), "The first estimation month should be after the last known index fixing");
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!