本文整理汇总了Java中java.time.LocalDateTime.parse方法的典型用法代码示例。如果您正苦于以下问题:Java LocalDateTime.parse方法的具体用法?Java LocalDateTime.parse怎么用?Java LocalDateTime.parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.time.LocalDateTime
的用法示例。
在下文中一共展示了LocalDateTime.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testCreateDefectLog
import java.time.LocalDateTime; //导入方法依赖的package包/类
@Test
public void testCreateDefectLog() {
PSP.root = root1;
// Normal creation, should yield no problems
LocalDateTime date = LocalDateTime.parse("2017-04-11 14:34", DateTimeFormatter.ofPattern(DATE_FORMAT));
DefectLog log1 = DefectLogManager.createDefectLog(date, 1, PSP.Defect.Data, PSP.Phase.Code, PSP.Phase.Code, 3, 1, "hi mom!");
assertNotNull(log1);
assertEquals(log1.getDescription(), "hi mom!");
// Null reference - should throw exception
boolean exThrown = false;
try {
DefectLog log2 = DefectLogManager.createDefectLog(null, 2, PSP.Defect.Assignment, PSP.Phase.Code, PSP.Phase.Code, 3, 1, "hi mom!");
} catch (NullPointerException e) {
exThrown = true;
} catch (Exception e2) {
// don't do anything??
}
assertTrue(exThrown);
}
示例2: deserialize
import java.time.LocalDateTime; //导入方法依赖的package包/类
@Override
public LocalDateTime deserialize(JsonParser parser, DeserializationContext context) throws IOException
{
if (parser.hasTokenId(JsonTokenId.ID_STRING)) {
String string = parser.getText().trim();
if (string.length() == 0) {
return null;
}
try {
return LocalDateTime.parse(string, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
} catch (DateTimeException e) {
throw new IOException(e);
}
}
if (parser.hasTokenId(JsonTokenId.ID_EMBEDDED_OBJECT)) {
return (LocalDateTime) parser.getEmbeddedObject();
}
throw context.wrongTokenException(parser, JsonToken.VALUE_STRING, "Expected string.");
}
示例3: parseDate
import java.time.LocalDateTime; //导入方法依赖的package包/类
public static LocalDateTime parseDate(String date) {
Objects.requireNonNull(date, "date");
for (DateTimeFormatter formatter : DATE_FORMATTERS) {
try {
// equals ISO_LOCAL_DATE
if (formatter.equals(DATE_FORMATTERS.get(2))) {
LocalDate localDate = LocalDate.parse(date, formatter);
return localDate.atStartOfDay();
} else {
return LocalDateTime.parse(date, formatter);
}
} catch (java.time.format.DateTimeParseException ignored) {
}
}
return null;
}
示例4: parseDate
import java.time.LocalDateTime; //导入方法依赖的package包/类
public static LocalDateTime parseDate(String str) throws ParseException {
if (str==null) return null;
// LSF puts in spaces for text alignment purposes instead of padding with zeros. These unfortunately break the
// date parser. The following is a relatively naive way to try to fix this, but it should work for all cases
// that we care about:
str = str.trim().replaceAll(" ", " 0");
// Remove the E for "Estimated" and other such characters from the end of dates.
str = str.replaceAll("( \\w)$", "");
// If necessary, add the current year (necessary before the 10.1.0.3 service pack)
String dateStr = str;
if (!dateStr.matches(".*\\d{4}")) {
dateStr = dateStr + " " + LocalDateTime.now().getYear();
}
if (dateStr.matches(".*\\d{2}:\\d{2}:\\d{2}.*")) {
return LocalDateTime.parse(dateStr, DATE_FORMAT_SECS);
}
return LocalDateTime.parse(dateStr, DATE_FORMAT);
}
示例5: main
import java.time.LocalDateTime; //导入方法依赖的package包/类
public static void main(String[] args) {
LocalDate date = LocalDate.of(2052, 01, 31);
System.out.println(date.minus(Period.ofDays(1)));
// Because Period instances can represent positive or negative periods
// (like 15 days and -15 days), you can subtract days from a LocalDate or
// LocalDateTime by calling the method plus
System.out.println(date.plus(Period.ofDays(-1)));
// Similarly, the method minus() with the classes LocalDate and LocalDateTime
// to subtract a period of years, month, weeks, or days
LocalDateTime dateTime = LocalDateTime.parse("2020-01-31T14:18:36");
System.out.println(dateTime.minus(Period.ofYears(2)));
LocalDate localDate = LocalDate.of(2052, 01, 31);
System.out.println(localDate.minus(Period.ofWeeks(4)));
}
示例6: main
import java.time.LocalDateTime; //导入方法依赖的package包/类
public static void main(String[] args) {
LocalDateTime prizeCeremony = LocalDateTime.parse("2050-06-05T14:00:00");
LocalDateTime dateTimeNow = LocalDateTime.now();
if (prizeCeremony.getMonthValue() == 6)
System.out.println("Can't invite president");
else
System.out.println("President invited");
LocalDateTime chiefGuestDeparture = LocalDateTime.parse("2050-06-05T14:30:00");
if (prizeCeremony.plusHours(2).isAfter(chiefGuestDeparture))
System.out.println("Chief Guest will leave before ceremony completes");
LocalDateTime eventMgrArrival = LocalDateTime.of(2050, 6, 5, 14, 14, 30, 0);
if (eventMgrArrival.isAfter(prizeCeremony.minusHours(3)))
System.out.println("Manager is supposed to arrive 3 hrs earlier");
}
示例7: resolveDate
import java.time.LocalDateTime; //导入方法依赖的package包/类
/**
* Helper method for resolving local date time. If the key to the data object doesn´t specify hours and minutes it
* returns the hour at beginning of that day.
*
* @param key the key to the data object, i.e: "2017-12-01 11:15" or "2017-12-01" depending on interval
* @return the {@link LocalDateTime} instance
*/
protected LocalDateTime resolveDate(String key) {
switch (interval) {
case MONTHLY:
case WEEKLY:
case DAILY:
return LocalDate.parse(key, SIMPLE_DATE_FORMAT).atStartOfDay();
default:
return LocalDateTime.parse(key, DATE_WITH_SIMPLE_TIME_FORMAT);
}
}
示例8: testfromBytesButDoNotSet
import java.time.LocalDateTime; //导入方法依赖的package包/类
@Test
public void testfromBytesButDoNotSet(){
String dateStr1 = "2016-06-22 19:20:14.100123456";
String dateStr2 = "2014-06-24 03:20:14.210998531";
DateTimeFormatter formatterWithNano = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSSSS");;
LocalDateTime localDateTime1 = LocalDateTime.parse(dateStr1, formatterWithNano);
LocalDateTime localDateTime2 = LocalDateTime.parse(dateStr2, formatterWithNano);
LocalDateTimeField field1 = new LocalDateTimeField(new LocalDateTimeFieldKey("test"), localDateTime1);
LocalDateTimeField field2 = new LocalDateTimeField(new LocalDateTimeFieldKey("test"), localDateTime2);
LocalDateTime localDateTimeFromBytes1 = field1.fromBytesButDoNotSet(field1.getBytes(), 0);
LocalDateTime localDateTimeFromBytes2 = field2.fromBytesButDoNotSet(field2.getBytes(), 0);
Assert.assertEquals(field1.getTruncatedLocalDateTime(localDateTime1), localDateTimeFromBytes1);
Assert.assertEquals(field2.getTruncatedLocalDateTime(localDateTime2), localDateTimeFromBytes2);
}
示例9: build
import java.time.LocalDateTime; //导入方法依赖的package包/类
public void build() {
Objects.requireNonNull(year);
Objects.requireNonNull(month);
Objects.requireNonNull(dayOfMonth);
Objects.requireNonNull(hour);
Objects.requireNonNull(minute);
StringBuilder dateString = new StringBuilder();
dateString.append(year);
dateString.append("-");
dateString.append(month);
dateString.append("-");
dateString.append(dayOfMonth);
dateString.append("T");
dateString.append(hour);
dateString.append(":");
dateString.append(minute);
dateString.append(":");
dateString.append(second);
if (nanosecond != null) {
dateString.append(".").append(nanosecond);
}
if (zoneOffset != null) {
dateString.append(zoneOffset);
}
if (zoneId != null) {
dateString.append(zoneId.trim());
}
boolean isZonedDateTime = zoneOffset != null || zoneId != null;
Temporal dateTime = isZonedDateTime ?
ZonedDateTime.parse(dateString.toString(), DateTimeFormatter.ISO_ZONED_DATE_TIME)
: LocalDateTime.parse(dateString.toString(), DateTimeFormatter.ISO_LOCAL_DATE_TIME);
set(dateTime);
}
示例10: factory_parse_formatter_nullText
import java.time.LocalDateTime; //导入方法依赖的package包/类
@Test(expectedExceptions=NullPointerException.class)
public void factory_parse_formatter_nullText() {
DateTimeFormatter f = DateTimeFormatter.ofPattern("y M d H m s");
LocalDateTime.parse((String) null, f);
}
示例11: unmarshal
import java.time.LocalDateTime; //导入方法依赖的package包/类
@Nullable
@Override
public LocalDateTime unmarshal(String v) {
return isEmpty(v) ? null : LocalDateTime.parse(v);
}
示例12: decodeText
import java.time.LocalDateTime; //导入方法依赖的package包/类
@Override
public LocalDateTime decodeText(int len, ByteBuf buff) {
CharSequence cs = buff.readCharSequence(len, StandardCharsets.UTF_8);
return LocalDateTime.parse(cs, TIMESTAMP_FORMAT);
}
示例13: read
import java.time.LocalDateTime; //导入方法依赖的package包/类
@Override
public LocalDateTime read(JsonReader in) throws IOException {
return LocalDateTime.parse(in.nextString());
}
示例14: setUp
import java.time.LocalDateTime; //导入方法依赖的package包/类
@Before
public void setUp() throws Exception {
// get testing values
estimatePhase = PSP.Phase.Code.toString();
actualPhase = PSP.Phase.Code.toString();
estimateMin = String.valueOf(90);
actualMin = String.valueOf(120);
// prep values
int estimateLoc = 0;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
String actualTimeStart = "2017-04-07 08:00";
LocalDateTime timeStart = LocalDateTime.parse(actualTimeStart, formatter);
LocalDateTime timeStop = timeStart.plusMinutes(Integer.parseInt(actualMin));
String actualTimeStop = timeStop.format(formatter);
// set testing values
Project project = ProjectManager.createProject("1","FirstProject",new CalendarDate(1,1,2017),new CalendarDate(1,1,2018));
Element root = new Element("psp");
Element projectNode = new Element(PSP.ELEMENT_NAME);
projectNode.addAttribute(new Attribute(PSP.ID_ATTRIBUTE, project.getID()));
// estimates
Element listNode = new Element(EstimateManager.ELEMENT_NAME);
Element estimateNode = new Element(EstimateManager.CHILD_ELEMENT);
estimateNode.addAttribute(new Attribute("module","test module"));
estimateNode.addAttribute(new Attribute("loc", String.valueOf(estimateLoc)));
estimateNode.addAttribute(new Attribute("phase", estimatePhase.toString()));
estimateNode.addAttribute(new Attribute("minutes", String.valueOf(estimateMin)));
estimateNode.addAttribute(new Attribute("comments","this is a test"));
// time logs
Element timeloglistNode = new Element("timeloglist");
Element yearNode = new Element("year");
yearNode.addAttribute(new Attribute("year", "2017"));
Element monthNode = new Element("month");
monthNode.addAttribute(new Attribute("month", "1"));
Element dayNode = new Element("day");
dayNode.addAttribute(new Attribute("day","1"));
dayNode.addAttribute(new Attribute("date", "1/1/2017"));
Element timelog = new Element("timelog");
timelog.addAttribute(new Attribute("id","1"));
timelog.addAttribute(new Attribute("phase", actualPhase.toString()));
timelog.addAttribute(new Attribute("interruption","0"));
timelog.addAttribute(new Attribute("comments","test"));
timelog.addAttribute(new Attribute("timestart",actualTimeStart));
timelog.addAttribute(new Attribute("timestop",actualTimeStop));
// build xml
root.appendChild(projectNode);
projectNode.appendChild(listNode);
projectNode.appendChild(timeloglistNode);
listNode.appendChild(estimateNode);
timeloglistNode.appendChild(yearNode);
yearNode.appendChild(monthNode);
monthNode.appendChild(dayNode);
dayNode.appendChild(timelog);
PSP.root = root;
testTimeTable = new TimeSummaryTable();
CurrentProject.set(project);
}
示例15: fromTaken
import java.time.LocalDateTime; //导入方法依赖的package包/类
public static LocalDateTime fromTaken(String dateTime){
return dateTime==null ? null : LocalDateTime.parse(dateTime, PICTURE_FORMATTER);
}