我正在创建一个存储java.time.LocalDate对象的堆程序.虽然我在为LocalDate创建临时变量时遇到了麻烦.我可能犯了一个小错误,但这是我的代码
private void trickleUp(int n) {
int pIndex;
if (n != 0) {
pIndex = getParentIndex(n);
if (heapA[pIndex].isAfter(heapA[n])) {
LocalDate temp = new LocalDate(heapA[pIndex]);
heapA[pIndex] = heapA[n];
heapA[n] = temp;
trickleUp(pIndex);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到错误'构造函数LocalDate(LocalDate)未定义'
我还没有找到一种在没有夏令时干扰的情况下获得时区偏移的方法。
LocalDateTime dt = LocalDateTime.now();
...
ZoneId zone = ZoneId.of(s);
ZonedDateTime zdt = dt.atZone(zone);
ZoneOffset offset = zdt.getOffset();
Run Code Online (Sandbox Code Playgroud)
这看起来是可行的,但初始 DataTime 可能会受到 DST 的影响。
还有这个:
int rawOffset = tZone.getRawOffset();
Run Code Online (Sandbox Code Playgroud)
然而,这总是一个正数,所以我们永远不会-05:00回来。
所以我想要的是一种在没有 DST 干扰的情况下获得偏移量的方法。有什么想法吗?
以下代码返回一个空字符串:
LocalDateTime.parse("2018-01-01T00:00:00.00")
.format(new DateTimeFormatterBuilder().toFormatter())
Run Code Online (Sandbox Code Playgroud)
我期望一个例外或以某种默认方式格式化的日期.
我是否在Java中发现了一个错误,或者这是否符合规范?我在Javadoc中找不到有关此行为的任何信息.
试图new Date().toString()用Java 支持Javascript的输出格式,DateTimeFormatter但似乎无法使其工作.
Js输出具有以下特性:
我目前的格式化程序:
int defaultOffset = ZonedDateTime.now().getOffset().getTotalSeconds();
DateTimeFormatter dtfJs = new DateTimeFormatterBuilder()
.appendPattern("EE MMM dd yyyy HH:mm:ss [OOOO (zzzz)]")
.parseDefaulting(ChronoField.OFFSET_SECONDS,defaultOffset
.toFormatter();
Run Code Online (Sandbox Code Playgroud)
如果我.parse()从js那些日期字符串,我得到以下错误:
[date]无法在索引25处解析
上述两个日期的指数25为:
我知道问题是:(冒号),因为如果我打印当前日期dtfJs,我得到:
Wed Apr 04 2018 10:25:10 GMT-05:00(哥伦比亚时间)
因此GMT-05:00,GMT-0500在收到的字符串中执行部分,但我找不到与此匹配的保留模式字母.
文档说:
偏移O:根据模式字母的数量格式化本地化偏移.一个字母输出本地化偏移的缩写形式,即局部偏移文本,例如'GMT',小时不带前导零,可选2位数分钟,第二个非零,和冒号,例如'GMT + 8 ".四个字母输出完整形式,这是本地化的偏移文本,例如'GMT,具有2位小时和分钟字段,可选的第二字段(如果非零)和冒号,例如'GMT + 08:00'.任何其他字母数量都会抛出IllegalArgumentException.
偏移Z:根据图案字母的数量格式化偏移.一个,两个或三个字母输出
小时和分钟,没有冒号,例如'+0130'.当偏移量为零时,输出将为"+0000".四个字母输出完整形式的局部偏移,相当于Offset-O的四个字母.如果偏移为零,则输出将是相应的本地化偏移文本.五个字母输出小时,分钟,可选第二个,如果非零,则使用冒号.如果偏移量为零,则输出"Z".六个或更多字母抛出IllegalArgumentException.
这意味着四个字母将始终以冒号":"输出,从而抛出 DateTimeParseException
非常感谢,谢谢 …
String ip="2011-05-01T06:47:35.422-05:00";
ZonedDateTime mzt = ZonedDateTime.parse(ip).toInstant().atZone(ZoneOffset.UTC);
System.out.println(mzt);
System.out.println("-----");
String ip2="2011-05-01T00:00:00.000-05:00";
ZonedDateTime mzt2 = ZonedDateTime.parse(ip2).toInstant().atZone(ZoneOffset.UTC);
System.out.println(mzt2);
Run Code Online (Sandbox Code Playgroud)
输出:
2011-05-01T11:47:35.422Z
-----
2011-05-01T05:00Z
Run Code Online (Sandbox Code Playgroud)
为什么在案例2中更改日期格式?由于这个原因,我收到SQLServer数据库错误.
在Java 8项目中,我可以这样解析日期:
LocalDate.parse(
newDateString,
DateTimeFormatter.ofPattern(expectedDateFormat)
)
.format(
DateTimeFormatter.ofPattern(expectedDBDateFormat)
);
Run Code Online (Sandbox Code Playgroud)
我的问题是,我有一个Java 6的项目,LocalDate并DateTimeFormatter没有为Java 6的定义.
如何在Java 6项目中解析日期?
我想创建方法,将属于某些trainingCycle的训练添加到日历中.可疑的是,我犯了一些错误,因为它只将培训添加到一天.
TrainingCycle模型类:
@DynamoDBTable(tableName = "trainingCycle")
public class TrainingCycle extends Request {
private String id;
private String cycleName;
private Long cycleTime;
private LocalDateTime startTime;
private List<List<Training>> trainingsForDay;
Run Code Online (Sandbox Code Playgroud)
培训模型类:
@DynamoDBTable(tableName = "training")
@DynamoDBDocument
public class Training extends Request {
private String id;
private String trainingName;
private Integer predictedCaloriesToBurn;
private Integer burnedCalories;
private LocalDateTime startTime;
private LocalDateTime endTime;
private List<Exercise> exerciseList;
private List<TrainingLabels> trainingLabels;
private boolean approved;
private ActivityDay activityDay;
Run Code Online (Sandbox Code Playgroud)
AddTrainingCycle方法:
public List<Day> addTrainingCycle(String userId, TrainingCycle trainingCycle, LocalDate localDate) {
List<Day> days = …Run Code Online (Sandbox Code Playgroud) 我发现听起来很难.
所以,我有一个最大日期和一个最小日期,我需要找到这两个日期之间的中位数日期.我使用Java 8来查找我的最大和最小日期,
LocalDate gerbutsmin = YearMonth.now().plusMonths(2).atDay(1);
LocalDate gerbutsmax = YearMonth.now().plusMonths(15).atDay(1);
Run Code Online (Sandbox Code Playgroud)
在此之后我将如何继续?也许我需要切换回Calander?
我有一个关于DateTimeFormatter格式化程序的问题。
在 SWAPI ( https://swapi.co/documentation#people ) 中,您可以阅读日期created和edited日期,格式如下:
2014-12-09T13:50:51.644000Z
Run Code Online (Sandbox Code Playgroud)
但在该文档类DateTimeFormatter在预定义的格式化程序部分,我不能看到SWAPI日期例子匹配任何格式。
问题是,当我尝试解析它时,正在使用SSSSSS:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'")
Run Code Online (Sandbox Code Playgroud)
与nnnnnn:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.nnnnnn'Z'")
Run Code Online (Sandbox Code Playgroud)
知道哪一个是正确的格式化程序吗?
我确实要检查一个Instant是否在其他两个瞬时之间:
目前,我使用:
import java.time.format.DateTimeFormatter;
import java.time.Instant;
Instant start = Instant.from(DateTimeFormatter.ISO_DATE_TIME.parse("2016-10-25T12:31:39.084726218Z"));
Instant end = Instant.from(DateTimeFormatter.ISO_DATE_TIME.parse("2016-10-25T13:31:39.084726218Z"));
// for exclusive range
Instant testSubject1 = Instant.from(DateTimeFormatter.ISO_DATE_TIME.parse("2016-10-25T12:31:40Z"));
boolean isInRange1 = testSubject1.isAfter(start) && testSubject1.isBefore(end); // this works as exclusive range
//for inclusive range
Instant testSubject2 = Instant.from(DateTimeFormatter.ISO_DATE_TIME.parse("2016-10-25T12:31:39.084726218Z"));
boolean isInRange2 = (testSubject2.equals(start) || testSubject2.isAfter(start)) && (testSubject2.equals(end) || testSubject2.isBefore(end)); // inclusive range
Run Code Online (Sandbox Code Playgroud)
标准库或其他地方是否还有其他实用程序功能可以简化这种范围检查?
我正在寻找类似的东西:
new InstantRange(start,end).checkInstantWithin(testSubject1);
// or
InstantUtils.inRangeExclusive(start,end, testSubject1);
InstantUtils.inRangeInclusivestart,end, testSubject1);
Run Code Online (Sandbox Code Playgroud) java ×10
java-time ×10
java-8 ×5
date ×3
datetime ×2
constructor ×1
date-parsing ×1
java-6 ×1
javascript ×1
junit ×1
timezone ×1
unit-testing ×1