本文整理汇总了Java中java.time.LocalDateTime.ofEpochSecond方法的典型用法代码示例。如果您正苦于以下问题:Java LocalDateTime.ofEpochSecond方法的具体用法?Java LocalDateTime.ofEpochSecond怎么用?Java LocalDateTime.ofEpochSecond使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.time.LocalDateTime
的用法示例。
在下文中一共展示了LocalDateTime.ofEpochSecond方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import java.time.LocalDateTime; //导入方法依赖的package包/类
/**
* 程序执行入口.
*
* @param args 命令行参数
*/
public static void main(String[] args) {
LocalDateTime today = LocalDateTime.now(); // |\longremark{获得当前的日期时间对象}|
System.out.println("Current DateTime=" + today);
//Current Date using LocalDate and LocalTime
today = LocalDateTime.of(LocalDate.now(), LocalTime.now());// |\longremark{根据给定的LocalDate和LocalTime创建日期时间对象}|
System.out.println("Current DateTime=" + today);
LocalDateTime specificDate = LocalDateTime.of(2014, 1, 1, 10, 10, 30);// |\longremark{根据给定的日期和时间创建日期时间对象}|
System.out.println("Specific Date=" + specificDate);
LocalDateTime todayShanghai = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));// |\longremark{根据给定的时区创建日期时间对象}|
System.out.println("Current Date in CST=" + todayShanghai);
LocalDateTime dateFromBase = LocalDateTime.ofEpochSecond(10000, 0, ZoneOffset.UTC);// |\longremark{从1970-1-1开始计算的日期时间对象}|
System.out.println("10000th second time from 01/01/1970= " + dateFromBase);
// LocalDateTime test1 = LocalDateTime.of(LocalDate.now(), null);
// System.out.println("test1:" + test1);
}
示例2: getPost
import java.time.LocalDateTime; //导入方法依赖的package包/类
public static Node getPost(Node author, Long time) {
LocalDateTime postedDateTime = LocalDateTime.ofEpochSecond(time, 0, ZoneOffset.UTC);
RelationshipType original = RelationshipType.withName("POSTED_ON_" +
postedDateTime.format(dateFormatter));
Node post = null;
for(Relationship r1 : author.getRelationships(Direction.OUTGOING, original)) {
Node potential = r1.getEndNode();
if (time.equals(potential.getProperty(TIME))) {
post = potential;
break;
}
}
if(post == null) { throw PostExceptions.postNotFound; };
return post;
}
示例3: testSetDateAt
import java.time.LocalDateTime; //导入方法依赖的package包/类
@Test
public void testSetDateAt() {
// 1987-04-15
long enc = 20 * 60;
LocalDateTime date = LocalDateTime.of(1984, 1, 1, 0, 0).plusSeconds(enc * 86400);
long S7_TIME_OFFSET = 441763200000L;
long millis = enc * 86400000L + S7_TIME_OFFSET;
Date date2 = new Date(millis);
assertEquals(date.toInstant(ZoneOffset.UTC), date2.toInstant());
LocalDateTime date3 = LocalDateTime.ofEpochSecond(millis / 1000, 0, ZoneOffset.UTC);
assertEquals(date, date3);
byte[] buffer = new byte[32];
Arrays.fill(buffer, (byte) 0);
LocalDateTime ldt = LocalDateTime.ofInstant(date.toInstant(ZoneOffset.UTC), ZoneOffset.systemDefault());
S7.setDateTimeAt(buffer, 0, ldt);
byte[] buffer1 = new byte[32];
Arrays.fill(buffer1, (byte) 0);
S7.setDateAt(buffer1, 0, date2);
Assert.assertArrayEquals(buffer, buffer1);
}
示例4: ofInstant
import java.time.LocalDateTime; //导入方法依赖的package包/类
/**
* Obtains an instance from an instant using the specified time-zone.
*
* @param chrono the chronology, not null
* @param instant the instant, not null
* @param zone the zone identifier, not null
* @return the zoned date-time, not null
*/
static ChronoZonedDateTimeImpl<?> ofInstant(Chronology chrono, Instant instant, ZoneId zone) {
ZoneRules rules = zone.getRules();
ZoneOffset offset = rules.getOffset(instant);
Objects.requireNonNull(offset, "offset"); // protect against bad ZoneRules
LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
ChronoLocalDateTimeImpl<?> cldt = (ChronoLocalDateTimeImpl<?>)chrono.localDateTime(ldt);
return new ChronoZonedDateTimeImpl<>(cldt, offset, zone);
}
示例5: getFollowing
import java.time.LocalDateTime; //导入方法依赖的package包/类
@GET
@Path("/{username}/following")
public Response getFollowing(@PathParam("username") final String username,
@QueryParam("limit") @DefaultValue("25") final Integer limit,
@QueryParam("since") final Long since,
@Context GraphDatabaseService db) throws IOException {
ArrayList<Map<String, Object>> results = new ArrayList<>();
LocalDateTime dateTime;
if (since == null) {
dateTime = LocalDateTime.now(utc);
} else {
dateTime = LocalDateTime.ofEpochSecond(since, 0, ZoneOffset.UTC);
}
Long latest = dateTime.toEpochSecond(ZoneOffset.UTC);
try (Transaction tx = db.beginTx()) {
Node user = findUser(username, db);
for (Relationship r1: user.getRelationships(Direction.OUTGOING, RelationshipTypes.FOLLOWS)) {
Long time = (Long)r1.getProperty(TIME);
if(time < latest) {
Node following = r1.getEndNode();
Map<String, Object> result = getUserAttributes(following);
result.put(TIME, time);
results.add(result);
}
}
tx.success();
}
results.sort(Comparator.comparing(m -> (Long) m.get(TIME), reverseOrder()));
return Response.ok().entity(objectMapper.writeValueAsString(
results.subList(0, Math.min(results.size(), limit))))
.build();
}
示例6: getResults
import java.time.LocalDateTime; //导入方法依赖的package包/类
@Override
public Stream<TtResult> getResults(Long ttId, Long from, Long to, ChronoUnit unit) {
LocalDateTime fromDate = LocalDateTime.ofEpochSecond(from, 0, ZoneOffset.UTC);
LocalDateTime toDate = LocalDateTime.ofEpochSecond(to, 0, ZoneOffset.UTC);
ViewResult results = bucket.query(
ViewQuery.from(DEFAULT_DESIGN_DOCUMENT, TT_RESULTS_BY_DATE_VIEW)
.startKey(JsonArray.from(
ttId,
fromDate.get(ChronoField.YEAR),
fromDate.get(ChronoField.MONTH_OF_YEAR),
fromDate.get(ChronoField.DAY_OF_MONTH),
fromDate.get(ChronoField.HOUR_OF_DAY),
fromDate.get(ChronoField.MINUTE_OF_HOUR),
fromDate.get(ChronoField.SECOND_OF_MINUTE)
))
.endKey(JsonArray.from(
ttId,
toDate.get(ChronoField.YEAR),
toDate.get(ChronoField.MONTH_OF_YEAR),
toDate.get(ChronoField.DAY_OF_MONTH),
toDate.get(ChronoField.HOUR_OF_DAY),
toDate.get(ChronoField.MINUTE_OF_HOUR),
toDate.get(ChronoField.SECOND_OF_MINUTE),
"\uEFFF"
))
.groupLevel(1 + CouchbaseUtils.chronoUnitToGroupLevel(unit))
);
return StreamSupport.stream(results.spliterator(), false)
.map(row -> row.value().toString())
.map(json -> gson.fromJson(json, TtResult.class));
}
示例7: TwoDayViewState
import java.time.LocalDateTime; //导入方法依赖的package包/类
public TwoDayViewState(TwoDayViewPresenter<G, I, D> presenter) {
this.presenter = presenter;
baseDate = LocalDateTime.ofEpochSecond(0, 0, ZoneOffset.UTC);
currDay = baseDate;
visibleDirty = true;
allDirty = true;
screenWidth = 1;
screenHeight = 1;
groupShifts = new HashMap<>();
scrollBarPos = 0;
scrollBarLength = 1;
scrollBarHandleLength = 1;
groupIndexOf = new HashMap<>();
}
示例8: asDateTime
import java.time.LocalDateTime; //导入方法依赖的package包/类
private static LocalDateTime asDateTime(Number value) {
if (value != null) {
if (TypeUtils.isIntegerNumber(value.getClass())) {
return LocalDateTime.ofEpochSecond(value.longValue(), 0, ZoneOffset.UTC);
}
}
return null;
}
示例9: getFollowers
import java.time.LocalDateTime; //导入方法依赖的package包/类
@GET
@Path("/{username}/followers")
public Response getFollowers(@PathParam("username") final String username,
@QueryParam("limit") @DefaultValue("25") final Integer limit,
@QueryParam("since") final Long since,
@Context GraphDatabaseService db) throws IOException {
ArrayList<Map<String, Object>> results = new ArrayList<>();
// TODO: 4/3/17 Add Recent Array for Users with > 100k Followers
LocalDateTime dateTime;
if (since == null) {
dateTime = LocalDateTime.now(utc);
} else {
dateTime = LocalDateTime.ofEpochSecond(since, 0, ZoneOffset.UTC);
}
Long latest = dateTime.toEpochSecond(ZoneOffset.UTC);
try (Transaction tx = db.beginTx()) {
Node user = findUser(username, db);
for (Relationship r1: user.getRelationships(Direction.INCOMING, RelationshipTypes.FOLLOWS)) {
Long time = (Long)r1.getProperty(TIME);
if(time < latest) {
Node follower = r1.getStartNode();
Map<String, Object> result = getUserAttributes(follower);
result.put(TIME, time);
results.add(result);
}
}
tx.success();
}
results.sort(Comparator.comparing(m -> (Long) m.get(TIME), reverseOrder()));
return Response.ok().entity(objectMapper.writeValueAsString(
results.subList(0, Math.min(results.size(), limit))))
.build();
}
示例10: getLatestTime
import java.time.LocalDateTime; //导入方法依赖的package包/类
public static Long getLatestTime(@QueryParam("since") Long since) {
LocalDateTime dateTime;
if (since == null) {
dateTime = LocalDateTime.now(utc);
} else {
dateTime = LocalDateTime.ofEpochSecond(since, 0, ZoneOffset.UTC);
}
return dateTime.toEpochSecond(ZoneOffset.UTC);
}
示例11: factory_ofEpochSecond_longOffset_afterEpoch
import java.time.LocalDateTime; //导入方法依赖的package包/类
@Test
public void factory_ofEpochSecond_longOffset_afterEpoch() {
LocalDateTime base = LocalDateTime.of(1970, 1, 1, 2, 0, 0, 500);
for (int i = 0; i < 100000; i++) {
LocalDateTime test = LocalDateTime.ofEpochSecond(i, 500, OFFSET_PTWO);
assertEquals(test, base.plusSeconds(i));
}
}
示例12: main
import java.time.LocalDateTime; //导入方法依赖的package包/类
public static void main(String[] args) throws Throwable {
int N = 10000;
long t1970 = new java.util.Date(70, 0, 01).getTime();
Random r = new Random();
for (int i = 0; i < N; i++) {
int days = r.nextInt(50) * 365 + r.nextInt(365);
long secs = t1970 + days * 86400 + r.nextInt(86400);
int nanos = r.nextInt(NANOS_PER_SECOND);
int nanos_ms = nanos / 1000000 * 1000000; // millis precision
long millis = secs * 1000 + r.nextInt(1000);
LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC);
LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC);
Instant inst = Instant.ofEpochSecond(secs, nanos);
Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms);
//System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
/////////// Timestamp ////////////////////////////////
Timestamp ta = new Timestamp(millis);
ta.setNanos(nanos);
if (!isEqual(ta.toLocalDateTime(), ta)) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
print(ta.toLocalDateTime(), ta);
throw new RuntimeException("FAILED: j.s.ts -> ldt");
}
if (!isEqual(ldt, Timestamp.valueOf(ldt))) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
print(ldt, Timestamp.valueOf(ldt));
throw new RuntimeException("FAILED: ldt -> j.s.ts");
}
Instant inst0 = ta.toInstant();
if (ta.getTime() != inst0.toEpochMilli() ||
ta.getNanos() != inst0.getNano() ||
!ta.equals(Timestamp.from(inst0))) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: j.s.ts -> instant -> j.s.ts");
}
inst = Instant.ofEpochSecond(secs, nanos);
Timestamp ta0 = Timestamp.from(inst);
if (ta0.getTime() != inst.toEpochMilli() ||
ta0.getNanos() != inst.getNano() ||
!inst.equals(ta0.toInstant())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: instant -> timestamp -> instant");
}
////////// java.sql.Date /////////////////////////////
// j.s.d/t uses j.u.d.equals() !!!!!!!!
java.sql.Date jsd = new java.sql.Date(millis);
if (!isEqual(jsd.toLocalDate(), jsd)) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
print(jsd.toLocalDate(), jsd);
throw new RuntimeException("FAILED: j.s.d -> ld");
}
LocalDate ld = ldt.toLocalDate();
if (!isEqual(ld, java.sql.Date.valueOf(ld))) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
print(ld, java.sql.Date.valueOf(ld));
throw new RuntimeException("FAILED: ld -> j.s.d");
}
////////// java.sql.Time /////////////////////////////
java.sql.Time jst = new java.sql.Time(millis);
if (!isEqual(jst.toLocalTime(), jst)) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
print(jst.toLocalTime(), jst);
throw new RuntimeException("FAILED: j.s.t -> lt");
}
// millis precision
LocalTime lt = ldt_ms.toLocalTime();
if (!isEqual(lt, java.sql.Time.valueOf(lt))) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
print(lt, java.sql.Time.valueOf(lt));
throw new RuntimeException("FAILED: lt -> j.s.t");
}
}
System.out.println("Passed!");
}
示例13: setEpochTimeMillis
import java.time.LocalDateTime; //导入方法依赖的package包/类
public void setEpochTimeMillis(long timestamp) throws IOException {
LocalDateTime localDateTime =
LocalDateTime.ofEpochSecond(timestamp / 1000, 0, ZoneOffset.UTC);
setUtcDateTime(localDateTime);
}
示例14: timestampToDateTime
import java.time.LocalDateTime; //导入方法依赖的package包/类
private static LocalDateTime timestampToDateTime(int timestamp) {
return LocalDateTime.ofEpochSecond(timestamp, 0, ZoneOffset.UTC);
}
示例15: factory_ofEpochSecond_badNanos_toSmall
import java.time.LocalDateTime; //导入方法依赖的package包/类
@Test(expectedExceptions=DateTimeException.class)
public void factory_ofEpochSecond_badNanos_toSmall() {
LocalDateTime.ofEpochSecond(0, -1, OFFSET_PONE);
}