Java Code Examples for java.time.LocalDateTime#get()

The following examples show how to use java.time.LocalDateTime#get() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: mokka7   File: S7.java    License: Eclipse Public License 1.0 6 votes vote down vote up
public static void setDateTimeAt(byte[] buffer, int pos, LocalDateTime dateTime) {
    int year, month, day, hour, min, sec;
    year = dateTime.get(ChronoField.YEAR);
    month = dateTime.get(ChronoField.MONTH_OF_YEAR);
    day = dateTime.get(ChronoField.DAY_OF_MONTH);
    hour = dateTime.get(ChronoField.HOUR_OF_DAY);
    min = dateTime.get(ChronoField.MINUTE_OF_HOUR);
    sec = dateTime.get(ChronoField.SECOND_OF_MINUTE);
    // milli = dateTime.get(ChronoField.MILLI_OF_SECOND);
    // // First two digits of miliseconds
    // int msecH = milli / 10;
    // // Last digit of miliseconds
    // int msecL = milli % 10;

    if (year > 1999) {
        year -= 2000;
    }
    buffer[pos] = byteToBCD(year);
    buffer[pos + 1] = byteToBCD(month);
    buffer[pos + 2] = byteToBCD(day);
    buffer[pos + 3] = byteToBCD(hour);
    buffer[pos + 4] = byteToBCD(min);
    buffer[pos + 5] = byteToBCD(sec);
    buffer[pos + 6] = byteToBCD(0);
    buffer[pos + 7] = byteToBCD(0);
}
 
Example 2
Source Project: scoold   File: Profile.java    License: Apache License 2.0 6 votes vote down vote up
private void updateVoteGains(int rep) {
	Long updated = Optional.ofNullable(getUpdated()).orElse(Utils.timestamp());
	LocalDateTime lastUpdate = LocalDateTime.ofInstant(Instant.ofEpochMilli(updated), ZoneId.systemDefault());
	LocalDate now = LocalDate.now();
	if (now.getYear() != lastUpdate.getYear()) {
		yearlyVotes = rep;
	} else {
		yearlyVotes += rep;
	}
	if (now.get(IsoFields.QUARTER_OF_YEAR) != lastUpdate.get(IsoFields.QUARTER_OF_YEAR)) {
		quarterlyVotes = rep;
	} else {
		quarterlyVotes += rep;
	}
	if (now.getMonthValue() != lastUpdate.getMonthValue()) {
		monthlyVotes = rep;
	} else {
		monthlyVotes += rep;
	}
	if (now.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR) != lastUpdate.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR)) {
		weeklyVotes = rep;
	} else {
		weeklyVotes += rep;
	}
}
 
Example 3
Source Project: pay-publicapi   File: RedisRateLimiter.java    License: MIT License 6 votes vote down vote up
/**
 * Derives Key (Service Key + Window) to use in Redis for noOfReq limiting.
 * Recommended to use perMillis to lowest granularity. i.e, to seconds. 1000, 2000
 * <p>
 * Depends on perMillis
 * <p>
 * - perMillis < 1000 : Window considered for milliseconds
 * - perMillis >=1000 && <=60000 : Window considered for seconds(s)
 *
 * @return new key based on perMillis (works for second/minute/hour windows only)
 */
private String getKeyForWindow(String key) throws OutOfScopeException {

    LocalDateTime now = LocalDateTime.now();

    int window;

    if (perMillis >= 1 && perMillis < 1000) {
        window = (now.get(ChronoField.MILLI_OF_DAY) / perMillis) + 1;
    } else if (perMillis >= 1000 && perMillis <= 60000) {
        window = now.get(ChronoField.SECOND_OF_MINUTE) / (perMillis / 1000);
    } else {
        throw new OutOfScopeException("perMillis specified is not currently supported");
    }

    return key + window;
}
 
Example 4
Source Project: java-trader   File: DateUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 规整时间的秒
 */
public static LocalDateTime round(LocalDateTime ldt) {
    int seconds = ldt.get(ChronoField.SECOND_OF_MINUTE);
    if (seconds >= 59) {
        ldt = ldt.plus(1, ChronoUnit.SECONDS);
    }
    return ldt.truncatedTo(ChronoUnit.SECONDS);
}
 
Example 5
Source Project: tablesaw   File: PackedLocalDateTime.java    License: Apache License 2.0 5 votes vote down vote up
public static int getWeekOfYear(long packedDateTime) {
  LocalDateTime date = asLocalDateTime(packedDateTime);
  if (date == null) {
    throw new IllegalArgumentException("Cannot get week of year for missing value");
  }
  TemporalField woy = WeekFields.of(Locale.getDefault()).weekOfWeekBasedYear();
  return date.get(woy);
}
 
Example 6
Source Project: tablesaw   File: PackedLocalDateTime.java    License: Apache License 2.0 5 votes vote down vote up
public static long getMillisecondOfDay(long packedLocalDateTime) {
  LocalDateTime localDateTime = PackedLocalDateTime.asLocalDateTime(packedLocalDateTime);
  if (localDateTime == null) {
    throw new IllegalArgumentException("Cannot get millisecond of day for missing value");
  }
  long total = (long) localDateTime.get(ChronoField.MILLI_OF_SECOND);
  total += localDateTime.getSecond() * 1000;
  total += localDateTime.getMinute() * 60 * 1000;
  total += localDateTime.getHour() * 60 * 60 * 1000;
  return total;
}
 
Example 7
Source Project: tablesaw   File: PackedLocalDateTime.java    License: Apache License 2.0 5 votes vote down vote up
public static int getWeekOfYear(long packedDateTime) {
  LocalDateTime date = asLocalDateTime(packedDateTime);
  if (date == null) {
    throw new IllegalArgumentException("Cannot get week of year for missing value");
  }
  TemporalField woy = WeekFields.of(Locale.getDefault()).weekOfWeekBasedYear();
  return date.get(woy);
}
 
Example 8
Source Project: tablesaw   File: PackedLocalDateTime.java    License: Apache License 2.0 5 votes vote down vote up
public static long getMillisecondOfDay(long packedLocalDateTime) {
  LocalDateTime localDateTime = PackedLocalDateTime.asLocalDateTime(packedLocalDateTime);
  if (localDateTime == null) {
    throw new IllegalArgumentException("Cannot get millisecond of day for missing value");
  }
  long total = (long) localDateTime.get(ChronoField.MILLI_OF_SECOND);
  total += localDateTime.getSecond() * 1000;
  total += localDateTime.getMinute() * 60 * 1000;
  total += localDateTime.getHour() * 60 * 60 * 1000;
  return total;
}
 
Example 9
Source Project: Java-Coding-Problems   File: Main.java    License: MIT License 4 votes vote down vote up
public static void main(String[] args) {

        System.out.println("Before JDK 8:");

        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        int yearC = calendar.get(Calendar.YEAR);
        int monthC = calendar.get(Calendar.MONTH);
        int weekC = calendar.get(Calendar.WEEK_OF_MONTH);
        int dayC = calendar.get(Calendar.DATE);
        int hourC = calendar.get(Calendar.HOUR_OF_DAY);
        int minuteC = calendar.get(Calendar.MINUTE);
        int secondC = calendar.get(Calendar.SECOND);
        int millisC = calendar.get(Calendar.MILLISECOND);

        System.out.println("Date: " + date);
        System.out.println("Year: " + yearC + " Month: " + monthC
                + " Week: " + weekC + " Day: " + dayC + " Hour: " + hourC
                + " Minute: " + minuteC + " Second: " + secondC + " Millis: " + millisC);

        System.out.println("\nStarting with JDK 8:");
        
        LocalDateTime ldt = LocalDateTime.now();
        
        int yearLDT = ldt.getYear();
        int monthLDT = ldt.getMonthValue();
        int dayLDT = ldt.getDayOfMonth();
        int hourLDT = ldt.getHour();
        int minuteLDT = ldt.getMinute();
        int secondLDT = ldt.getSecond();
        int nanoLDT = ldt.getNano();                

        System.out.println("LocalDateTime: " + ldt);
        System.out.println("Year: " + yearLDT + " Month: " + monthLDT
                + " Day: " + dayLDT + " Hour: " + hourLDT + " Minute: " + minuteLDT 
                + " Second: " + secondLDT + " Nano: " + nanoLDT);
        
        int yearLDT2 = ldt.get(ChronoField.YEAR);
        int monthLDT2 = ldt.get(ChronoField.MONTH_OF_YEAR);
        int dayLDT2 = ldt.get(ChronoField.DAY_OF_MONTH);
        int hourLDT2 = ldt.get(ChronoField.HOUR_OF_DAY);
        int minuteLDT2 = ldt.get(ChronoField.MINUTE_OF_HOUR);
        int secondLDT2 = ldt.get(ChronoField.SECOND_OF_MINUTE);
        int nanoLDT2 = ldt.get(ChronoField.NANO_OF_SECOND);                
        
        System.out.println("Year: " + yearLDT2 + " Month: " + monthLDT2
                + " Day: " + dayLDT2 + " Hour: " + hourLDT2 + " Minute: " + minuteLDT2
                + " Second: " + secondLDT2 + " Nano: " + nanoLDT2);
    }
 
Example 10
Source Project: redtorch   File: XSecBarGenerator.java    License: MIT License 4 votes vote down vote up
/**
 * 更新Tick数据
 * 
 * @param tick
 */
public void updateTick(TickField tick) {

	// 如果tick为空或者合约不匹配则返回
	if (tick == null) {
		logger.warn("输入的Tick数据为空,当前Bar合约{}",barUnifiedSymbol);
		return;
	}

	if (barUnifiedSymbol == null) {
		barUnifiedSymbol = tick.getUnifiedSymbol();
	} else if (!barUnifiedSymbol.equals(tick.getUnifiedSymbol())) {
		logger.warn("合约不匹配,当前Bar合约{}",barUnifiedSymbol);
		return;
	}

	LocalDateTime tickLocalDateTime = CommonUtils.millsToLocalDateTime(tick.getActionTimestamp());

	// 此处过滤用于一个策略在多个网关订阅了同一个合约的情况下,Tick到达顺序和实际产生顺序不一致或者重复的情况
	if (lastTickLocalDateTime != null && tickLocalDateTime.isBefore(lastTickLocalDateTime)) {
		logger.warn("时间乱序,当前Bar合约{}",barUnifiedSymbol);
		return;
	}

	lastTickLocalDateTime = tickLocalDateTime;
	
	if (preTick != null) {
		// 如果切换交易日
		if (!preTick.getTradingDay().equals(tick.getTradingDay())) {
			preTick = null;
			if(barBuilder!=null){
				finish();
			}
		}else if(!preTick.getActionDay().equals(tick.getActionDay())) {
			if(barBuilder!=null){
				finish();
			}
		}
	}

	if (barBuilder == null) {
		barBuilder = BarField.newBuilder();
		newFlag = true;
	} else if (((int) (barLocalDateTime.get(ChronoField.SECOND_OF_DAY) / xSeconds) != (int) (tickLocalDateTime.get(ChronoField.SECOND_OF_DAY) / xSeconds))) {
		finish();
		newFlag = true;
		barBuilder = BarField.newBuilder();
	} else {
		newFlag = false;
	}

	if (newFlag) {
		barBuilder.setUnifiedSymbol(tick.getUnifiedSymbol());
		barBuilder.setGatewayId(tick.getGatewayId());
		barBuilder.setTradingDay(tick.getTradingDay());
		barBuilder.setActionDay(tick.getActionDay());

		barBuilder.setOpenPrice(tick.getLastPrice());
		barBuilder.setHighPrice(tick.getLastPrice());
		barBuilder.setLowPrice(tick.getLastPrice());

		barLocalDateTime = tickLocalDateTime;
	} else {
		// 当日最高价发生变动
		if (preTick != null && !CommonUtils.isEquals(tick.getHighPrice(), preTick.getHighPrice())) {
			barBuilder.setHighPrice(tick.getHighPrice());
		} else {
			barBuilder.setHighPrice(Math.max(barBuilder.getHighPrice(), tick.getLastPrice()));
		}

		// 当日最低价发生变动
		if (preTick != null && !CommonUtils.isEquals(tick.getLowPrice(), preTick.getLowPrice())) {
			barBuilder.setLowPrice(tick.getLowPrice());
		} else {
			barBuilder.setLowPrice(Math.min(barBuilder.getLowPrice(), tick.getLastPrice()));
		}

	}

	barBuilder.setClosePrice(tick.getLastPrice());
	barBuilder.setOpenInterest(tick.getOpenInterest());
	barBuilder.setVolume(tick.getVolume());
	barBuilder.setTurnover(tick.getTurnover());

	if (preTick != null) {
		barBuilder.setVolumeDelta(tick.getVolume() - preTick.getVolume() + barBuilder.getVolumeDelta());
		barBuilder.setTurnoverDelta(tick.getTurnover() - preTick.getTurnover() + barBuilder.getTurnoverDelta());
		barBuilder.setOpenInterestDelta(tick.getOpenInterest() - preTick.getOpenInterest() + barBuilder.getOpenInterestDelta());
	} else {
		barBuilder.setVolumeDelta(tick.getVolume());
		barBuilder.setTurnoverDelta(tick.getTurnover());
		barBuilder.setOpenInterestDelta(tick.getOpenInterest()-tick.getPreOpenInterest());
	}

	preTick = tick;
}
 
Example 11
Source Project: redtorch   File: BarGenerator.java    License: MIT License 4 votes vote down vote up
/**
 * 更新Tick数据
 * 
 * @param tick
 */
public void updateTick(TickField tick) {

	// 如果tick为空或者合约不匹配则返回
	if (tick == null) {
		logger.warn("输入的Tick数据为空,当前Bar合约{}",barUnifiedSymbol);
		return;
	}

	if (barUnifiedSymbol == null) {
		barUnifiedSymbol = tick.getUnifiedSymbol();
	} else if (!barUnifiedSymbol.equals(tick.getUnifiedSymbol())) {
		logger.warn("合约不匹配,当前Bar合约{}",barUnifiedSymbol);
		return;
	}

	LocalDateTime tickLocalDateTime = CommonUtils.millsToLocalDateTime(tick.getActionTimestamp());

	// 此处过滤用于一个策略在多个网关订阅了同一个合约的情况下,Tick到达顺序和实际产生顺序不一致或者重复的情况
	if (lastTickLocalDateTime != null && tickLocalDateTime.isBefore(lastTickLocalDateTime)) {
		logger.warn("时间乱序,当前Bar合约{}",barUnifiedSymbol);
		return;
	}

	lastTickLocalDateTime = tickLocalDateTime;

	if (preTick != null) {
		// 如果切换交易日
		if (!preTick.getTradingDay().equals(tick.getTradingDay())) {
			preTick = null;
			if(barBuilder!=null){
				finish();
			}
		}else if(!preTick.getActionDay().equals(tick.getActionDay())) {
			if(barBuilder!=null){
				finish();
			}
		}
	}

	if (barBuilder == null) {
		barBuilder = BarField.newBuilder();
		newFlag = true;
	} else if (barLocalDateTime.get(ChronoField.MINUTE_OF_DAY) != tickLocalDateTime.get(ChronoField.MINUTE_OF_DAY) 
			|| (preTick != null && !preTick.getTradingDay().equals(tick.getTradingDay()))) {
		finish();
		barBuilder = BarField.newBuilder();
	} else {
		newFlag = false;
	}

	if (newFlag) {
		barBuilder.setUnifiedSymbol(tick.getUnifiedSymbol());
		barBuilder.setGatewayId(tick.getGatewayId());
		barBuilder.setTradingDay(tick.getTradingDay());
		barBuilder.setActionDay(tick.getActionDay());

		barBuilder.setOpenPrice(tick.getLastPrice());
		barBuilder.setHighPrice(tick.getLastPrice());
		barBuilder.setLowPrice(tick.getLastPrice());

		barLocalDateTime = tickLocalDateTime;
	} else {
		// 当日最高价发生变动
		if (preTick != null && !CommonUtils.isEquals(tick.getHighPrice(), preTick.getHighPrice())) {
			barBuilder.setHighPrice(tick.getHighPrice());
		} else {
			barBuilder.setHighPrice(Math.max(barBuilder.getHighPrice(), tick.getLastPrice()));
		}

		// 当日最低价发生变动
		if (preTick != null && !CommonUtils.isEquals(tick.getLowPrice(), preTick.getLowPrice())) {
			barBuilder.setLowPrice(tick.getLowPrice());
		} else {
			barBuilder.setLowPrice(Math.min(barBuilder.getLowPrice(), tick.getLastPrice()));
		}

	}

	barBuilder.setClosePrice(tick.getLastPrice());
	barBuilder.setOpenInterest(tick.getOpenInterest());
	barBuilder.setVolume(tick.getVolume());
	barBuilder.setTurnover(tick.getTurnover());

	if (preTick != null) {
		barBuilder.setVolumeDelta(tick.getVolume() - preTick.getVolume() + barBuilder.getVolumeDelta());
		barBuilder.setTurnoverDelta(tick.getTurnover() - preTick.getTurnover() + barBuilder.getTurnoverDelta());
		barBuilder.setOpenInterestDelta(tick.getOpenInterest() - preTick.getOpenInterest() + barBuilder.getOpenInterestDelta());
	} else {
		barBuilder.setVolumeDelta(tick.getVolume());
		barBuilder.setTurnoverDelta(tick.getTurnover());
		barBuilder.setOpenInterestDelta(tick.getOpenInterest()-tick.getPreOpenInterest());
	}

	preTick = tick;
}
 
Example 12
Source Project: redtorch   File: XMinBarGenerator.java    License: MIT License 4 votes vote down vote up
public void updateBar(BarField bar) {
	
	// 如果bar为空或者合约不匹配则返回
	if (bar == null) {
		logger.warn("输入的Bar数据为空,当前XMinBar合约{}",xMinBarUnifiedSymbol);
		return;
	}

	if (xMinBarUnifiedSymbol == null) {
		xMinBarUnifiedSymbol = bar.getUnifiedSymbol();
	} else if (!xMinBarUnifiedSymbol.equals(bar.getUnifiedSymbol())) {
		logger.warn("合约不匹配,当前XMinBar合约{}",xMinBarUnifiedSymbol);
		return;
	}

	LocalDateTime barLocalDateTime = CommonUtils.millsToLocalDateTime(bar.getActionTimestamp());

	if(lastBarLocalDateTime!=null && barLocalDateTime.isBefore(lastBarLocalDateTime)) {
		logger.warn("时间乱序,当前XMinBar合约{}",xMinBarUnifiedSymbol);
	}
	
	lastBarLocalDateTime = barLocalDateTime;
	
	// 如果交易日发生了变更
	if(xMinBarBuilder!=null&&!xMinBarBuilder.getTradingDay().equals(bar.getTradingDay())) {
		finish();
	}
	
	// 如果日期发生了变更
	if(xMinBarBuilder!=null&&!xMinBarBuilder.getActionDay().equals(bar.getActionDay())) {
		finish();
	}
	
	// 如果已经不属于同一个周期
	if(xMinBarBuilder!=null&&(int)xMinBarLocalDateTime.get(ChronoField.MINUTE_OF_DAY)/xMin != (int)barLocalDateTime.get(ChronoField.MINUTE_OF_DAY)/xMin) {
		finish();
	}

	if (xMinBarBuilder == null) {
		xMinBarBuilder = BarField.newBuilder();
		xMinBarBuilder.setUnifiedSymbol(bar.getUnifiedSymbol());
		xMinBarBuilder.setGatewayId(bar.getGatewayId());

		xMinBarBuilder.setTradingDay(bar.getTradingDay());
		xMinBarBuilder.setActionDay(bar.getActionDay());

		xMinBarBuilder.setOpenPrice(bar.getOpenPrice());
		xMinBarBuilder.setHighPrice(bar.getHighPrice());
		xMinBarBuilder.setLowPrice(bar.getLowPrice());

		xMinBarLocalDateTime = CommonUtils.millsToLocalDateTime(bar.getActionTimestamp());

	} else {
		xMinBarBuilder.setHighPrice(Math.max(xMinBarBuilder.getHighPrice(), bar.getHighPrice()));
		xMinBarBuilder.setLowPrice(Math.min(xMinBarBuilder.getLowPrice(), bar.getLowPrice()));
	}

	xMinBarBuilder.setClosePrice(bar.getClosePrice());
	xMinBarBuilder.setVolume(bar.getVolume());
	xMinBarBuilder.setTurnover(bar.getTurnover());
	xMinBarBuilder.setOpenInterest(bar.getOpenInterest());
	xMinBarBuilder.setVolumeDelta(xMinBarBuilder.getVolumeDelta() + bar.getVolumeDelta());
	xMinBarBuilder.setTurnoverDelta(xMinBarBuilder.getTurnoverDelta() + bar.getTurnoverDelta());
	xMinBarBuilder.setOpenInterestDelta(xMinBarBuilder.getOpenInterestDelta() + bar.getOpenInterestDelta());

	// 如果当前周期结束
	if ((barLocalDateTime.get(ChronoField.MINUTE_OF_DAY) + 1) % xMin == 0) {
		finish();
	}
}
 
Example 13
Source Project: dremio-oss   File: TimePrintMillis.java    License: Apache License 2.0 4 votes vote down vote up
public TimePrintMillis(LocalDateTime time) {
  super(time.getHour(), time.getMinute(), time.getSecond());
  millisOfSecond = time.get(ChronoField.MILLI_OF_SECOND);
}
 
Example 14
Source Project: core-ng-project   File: AlertService.java    License: Apache License 2.0 4 votes vote down vote up
String color(Severity severity, LocalDateTime now) {
    int week = now.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR);
    int colorIndex = severity == Severity.WARN ? 0 : 1;
    return colors[colorIndex][(week - 1) % 2];
}
 
Example 15
Source Project: jackcess   File: DefaultDateFunctions.java    License: Apache License 2.0 4 votes vote down vote up
private static int weekOfYear(LocalDateTime ldt, int firstDay,
                              int firstWeekType) {
  WeekFields weekFields = weekFields(firstDay, firstWeekType);
  return ldt.get(weekFields.weekOfWeekBasedYear());
}
 
Example 16
Source Project: game-server   File: TimeUtil.java    License: MIT License 3 votes vote down vote up
/**
 * 判断两个时间是否在同一周(注意这里周日和周一判断是在一周里的)
 *
 * @param time1
 * @param time2
 * @return
 */
public static boolean isSameWeek(long time1, long time2) {
    LocalDateTime ldt1 = LocalDateTime.ofInstant(Instant.ofEpochMilli(time1), ZoneId.systemDefault());
    LocalDateTime ldt2 = LocalDateTime.ofInstant(Instant.ofEpochMilli(time2), ZoneId.systemDefault());
    TemporalField woy = WeekFields.of(Locale.getDefault()).weekOfWeekBasedYear();
    return ldt1.getYear() == ldt2.getYear() && ldt1.get(woy) == ldt2.get(woy);
}