Momentjs calendar()等效于Java
Momentjs具有Momentjs calendar()等效于Java,java,momentjs,jodatime,date-difference,Java,Momentjs,Jodatime,Date Difference,Momentjs具有calendar()函数,可以很好地打印时间跨度,如下所示 例如: “上周一下午1:14” “2017年9月21日”(如果日期是前一段时间) Java中是否有等价的函数(如果可能的话是Joda Time) java.time 现代方法使用业界领先的java.time类 ZoneId z = ZoneId.now( “America/Montreal” ) ; LocalDate today = LocalDate.now( z ) ; 使用加号和减号方法进行数学运算 Lo
calendar()
函数,可以很好地打印时间跨度,如下所示
例如:
“上周一下午1:14”
“2017年9月21日”(如果日期是前一段时间)
Java中是否有等价的函数(如果可能的话是Joda Time)
java.time
现代方法使用业界领先的java.time类
ZoneId z = ZoneId.now( “America/Montreal” ) ;
LocalDate today = LocalDate.now( z ) ;
使用加号和减号方法进行数学运算
LocalDate tomorrow = today.plusDays( 1 ) ;
搜索堆栈溢出。几乎每一个基本的日期时间问题都已经被问到了
串
time类不会生成诸如“明天”和“上周一”之类的字符串。因此,没有直接等效于您的引用库。你得自己干农活
搜索该类,然后搜索该类
此外,枚举及其自动本地化方法也可能有用
美丽时光
图书馆可能会帮助你,尽管我没用过
乔达时间
Joda Time项目处于维护模式。团队建议迁移到java.time类
关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,& 该项目现已启动,建议迁移到类 要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是 从哪里获得java.time类
- ,及以后
- 内置的李>
- 标准JavaAPI的一部分,带有捆绑实现
- Java9添加了一些次要功能和修复
- 及
- 大部分java.time功能都在中向后移植到Java6和Java7
-
- 该项目专门为Android采用了ThreeTen Backport(如上所述)
- 看
该项目使用其他类扩展了java.time。这个项目是java.time将来可能添加的一个试验场。您可能会在这里找到一些有用的类,如、、和。Java中没有内置函数,但使用现有的API并不难做到这一点。首先,您需要获得今天的差异(以天为单位),并根据以下内容选择适当的字符串: 在Joda Time中,您可以使用
org.Joda.Time.Days
类来获取天数差异。一个细节是,我只考虑了日期(天/月/年),而忽略了获得差异的时间(小时/分钟/秒)(但您可以根据需要进行调整)。方法如下所示:
public String calendar(DateTime dt) {
StringBuilder sb = new StringBuilder();
// check difference in days from today, considering just the date (ignoring the hours)
int days = Days.daysBetween(new LocalDate(), dt.toLocalDate()).getDays();
if (days == 0) { // today
sb.append("Today ");
} else if (days == 1) { // tomorrow
sb.append("Tomorrow ");
} else if (days == -1) { // yesterday
sb.append("Yesterday ");
} else if (days > 0 && days < 7) { // next week
sb.append(dt.dayOfWeek().getAsText(Locale.ENGLISH)).append(" ");
} else if (days < 0 && days > -7) { // last week
sb.append("Last ").append(dt.dayOfWeek().getAsText(Locale.ENGLISH)).append(" ");
}
if (Math.abs(days) < 7) { // difference is less than a week, append current time
sb.append("at ").append(dt.toString("h:mm a", Locale.ENGLISH));
} else { // more than a week of difference
sb.append(dt.toString("M/d/yyyy"));
}
return sb.toString();
}
输出是(考虑到今天是2017年10月2日,我在当地时间上午9:34运行代码):
2017年9月22日上周二上午9:34
上周五上午9:34
昨天上午9:34
今天上午9:34
明天上午9:34
周四上午9:34
2017年10月12日 您还可以修改该方法以获取要比较的参考日期(而不是在该方法中使用硬编码的
newlocaldate()
)
Java新的日期/时间API Joda Time处于维护模式,正在被新的API所取代,因此我不建议使用它启动新项目。甚至在文章中也提到:“请注意,Joda Time被认为是一个基本上“完成”的项目。没有计划进行重大的增强。如果使用Java SE 8,请迁移到Java.Time(JSR-310)。” 如果使用<强> java 8 < />,请考虑使用这更容易 如果您使用的是Java6或7,则可以使用,这是Java8新的日期/时间类的一个很好的后端口。对于Android,您还需要(更多关于如何使用它的信息) 下面的代码适用于这两种情况。 唯一的区别是包名(在Java8中是
Java.time
,在ThreeTen Backport(或Android的ThreeTenABP)中是org.ThreeTen.bp
),但类和方法的名称是相同的
此API与Joda Time非常相似,因此代码存在细微差异:
static DateTimeFormatter HOUR_FORMAT = DateTimeFormatter.ofPattern("h:mm a", Locale.ENGLISH);
static DateTimeFormatter MDY_FORMAT = DateTimeFormatter.ofPattern("M/d/yyyy");
public String calendar(ZonedDateTime dt) {
StringBuilder sb = new StringBuilder();
// check difference in days from today, considering just the date (ignoring the hours)
long days = ChronoUnit.DAYS.between(LocalDate.now(), dt.toLocalDate());
if (days == 0) { // today
sb.append("Today ");
} else if (days == 1) { // tomorrow
sb.append("Tomorrow ");
} else if (days == -1) { // yesterday
sb.append("Yesterday ");
} else if (days > 0 && days < 7) { // next week
sb.append(dt.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH)).append(" ");
} else if (days < 0 && days > -7) { // last week
sb.append("Last ").append(dt.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH)).append(" ");
}
if (Math.abs(days) < 7) { // difference is less than a week, append current time
sb.append("at ").append(dt.format(HOUR_FORMAT));
} else { // more than a week of difference
sb.append(dt.format(MDY_FORMAT));
}
return sb.toString();
}
只是一个关于时区的说明。在
java.time
API中,所有日期类都有一个no-argnow()
方法,该方法获取JVM默认时区中的当前日期/时间
虽然它非常方便,但也有一些缺点,因为默认时区可以在不通知的情况下更改,即使在运行时也是如此。如果可能的话,最好指定您想要的时区
API使用的格式(始终为地区/城市
,如美国/纽约
或欧洲/巴黎
)。
避免使用三个字母的缩写(如CET
或EST
),因为它们是
通过调用ZoneId.getAvailableZoneIds()
,可以获得可用时区的列表(并选择最适合您的系统的时区)
要获取特定时区中的当前日期/时间,请使用ZoneId
类:
// get the current date/time in a specific timezone
ZonedDateTime.now(ZoneId.of("Europe/Paris"));
我不是这么问的。我的问题是:我想打印一些日期,比如“上周一下午1:14”、“明天下午1:14”、“2017年11月10日”(如果是很久以前的话)automatically@PerrierCitror啊,我没有意识到这些注释字符串是目标。看我的编辑。嗯,看起来没有直接的等价物。。。我讨厌Java中的时间格式化。。。这样的痛苦manFYI,Joda Time项目处于维护模式。团队建议迁移到java.time类。
DateTime now = new DateTime();
System.out.println(calendar(now.minusDays(10))); // 9/22/2017
System.out.println(calendar(now.minusDays(6))); // Last Tuesday at 9:34 AM
System.out.println(calendar(now.minusDays(3))); // Last Friday at 9:34 AM
System.out.println(calendar(now.minusDays(1))); // Yesterday at 9:34 AM
System.out.println(calendar(now)); // Today at 9:34 AM
System.out.println(calendar(now.plusDays(1))); // Tomorrow at 9:34 AM
System.out.println(calendar(now.plusDays(3))); // Thursday at 9:34 AM
System.out.println(calendar(now.plusDays(10))); // 10/12/2017
static DateTimeFormatter HOUR_FORMAT = DateTimeFormatter.ofPattern("h:mm a", Locale.ENGLISH);
static DateTimeFormatter MDY_FORMAT = DateTimeFormatter.ofPattern("M/d/yyyy");
public String calendar(ZonedDateTime dt) {
StringBuilder sb = new StringBuilder();
// check difference in days from today, considering just the date (ignoring the hours)
long days = ChronoUnit.DAYS.between(LocalDate.now(), dt.toLocalDate());
if (days == 0) { // today
sb.append("Today ");
} else if (days == 1) { // tomorrow
sb.append("Tomorrow ");
} else if (days == -1) { // yesterday
sb.append("Yesterday ");
} else if (days > 0 && days < 7) { // next week
sb.append(dt.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH)).append(" ");
} else if (days < 0 && days > -7) { // last week
sb.append("Last ").append(dt.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH)).append(" ");
}
if (Math.abs(days) < 7) { // difference is less than a week, append current time
sb.append("at ").append(dt.format(HOUR_FORMAT));
} else { // more than a week of difference
sb.append(dt.format(MDY_FORMAT));
}
return sb.toString();
}
ZonedDateTime now = ZonedDateTime.now();
System.out.println(calendar(now.minusDays(10))); // 9/22/2017
System.out.println(calendar(now.minusDays(6))); // Last Tuesday at 9:34 AM
System.out.println(calendar(now.minusDays(3))); // Last Friday at 9:34 AM
System.out.println(calendar(now.minusDays(1))); // Yesterday at 9:34 AM
System.out.println(calendar(now)); // Today at 9:34 AM
System.out.println(calendar(now.plusDays(1))); // Tomorrow at 9:34 AM
System.out.println(calendar(now.plusDays(3))); // Thursday at 9:34 AM
System.out.println(calendar(now.plusDays(10))); // 10/12/2017
// get the current date/time in a specific timezone
ZonedDateTime.now(ZoneId.of("Europe/Paris"));