标签 副本 下的文章

从 Java 5.0 开始,String 类新增了一个强大的字符串格式化方法 format()。这个方法到现在用的人还是不多,实在是一种浪费。本文带你快速过一遍这个方法的功能,将来你要用到格式化文本的时候,可能就不需要再借用第三方类库或自己去实现了。

首先看一个简单例子:

String formatted = String.format("%s今年%d岁。", "小李", 30); // "小李今年30岁。"

不用我多解释,你也可以看出:

  1. 这个方法第一个参数是格式串,后面的参数都是格式串的参数,用于替换格式串中的占位符。
  2. 占位符以 "%x" 的形式表示,不同的参数类型要用不同的字母。后面会具体介绍。
  3. String.format() 返回值类型为字符串,也就是格式化的结果。

- 阅读剩余部分 -

常规类型的格式化

String类的format()方法用于创建格式化的字符串以及连接多个字符串对象。熟悉C语言的同学应该记得C语言的sprintf()方法,两者有类似之处。format()方法有两种重载形式。

format(String format, Object... args) 新字符串使用本地语言环境,制定字符串格式和参数生成格式化的新字符串。

format(Locale locale, String format, Object... args) 使用指定的语言环境,制定字符串格式和参数生成格式化的字符串。

- 阅读剩余部分 -

If you want logical calendar days, use DAYS.between() method from java.time.temporal.ChronoUnit:

LocalDate dateBefore;
LocalDate dateAfter;
long daysBetween = DAYS.between(dateBefore, dateAfter);

If you want literal 24 hour days, (a duration), you can use the Duration class instead:

LocalDate today = LocalDate.now()
LocalDate yesterday = today.minusDays(1);
// Duration oneDay = Duration.between(today, yesterday); // throws an exception
Duration.between(today.atStartOfDay(), yesterday.atStartOfDay()).toDays() // another option

For more information, refer to this document.

https://stackoverflow.com/questions/27005861/calculate-days-between-two-dates-in-java-8

在本指南中,我们将了解如何将 LocalDate 转换为 Date。在查看转换代码之前,让我们先看看 Date 和 LocalDate 之间有什么区别。

java.util.Date - date + time + timezone
java.time.LocalDate - only date

因此,要将 LocalDate 转换为 Date,我们必须在日期后附加时间和时区信息。考虑到这一点,转换步骤如下:

  1. 获取默认时区,以便在日期中附加时区信息
  2. 调用 atStartOfDay(),以便将时间与日期附加在一起
  3. LocalDate to Date - local date + atStartOfDay() + 默认时区(default time zone) + toInstant() = Date

- 阅读剩余部分 -

As nobody answered, So I asked this on MongoDB-User Group

Say cars is your outputcollection (where you want to store output of map-reduce job).

  • REPLACE: If your current MR is successful, all of the documents in cars will be deleted (regardless of their _id) and replaced by the current result.
  • MERGE: No document in cars will be deleted. Instead, each document in your current result will replace an already existing document in cars with the same _id. If there is not any document in cars with that _id, it will be just inserted. You can see this like an upsert:

    db.cars.update({_id: newDocument._id}, {value: newDocument.value}, {upsert: true})
  • REDUCE: This is very similar to MERGE. But instead of just replacing the existing document, both documents will be the input of your reduce function (i.e. reduce([oldDocument, newDocument])) and the resulting document will replace the existing one.
  • INLINE: Returns your result as a variable in the same fashion a function does. Nothing is stored in MongoDB, so this does not impact any collection.

The complete answer can be found here.

https://stackoverflow.com/questions/30508245/what-does-mapreducecommand-outputtype-reduce-do-in-mapreduce-command-in-mongodb