我想使用Date和Calendar类来计算java中两个日期/时间之间的差异.我的格式是"2012-01-24 12:30:00 PM".
我已经实现了我自己的方法,并且谷歌它与其他人合作,但没有得到正确的提示来处理AM和PM值.
只要时间有12(上午/下午),日期/时间差就会受到影响.例如,如果我有日期/时间"2012-01-24 12:30:00 PM"和"2012-01-24 02:30:00 PM",则显示差异为10小时.
考虑到此链接上的代码,如何修改它以处理AM和PM.
要将字符串日期转换为日期,请使用以下代码:
String sessionTimeStart = "2012-01-24 12:30:00 PM";
String sessionTimerEndOrCurrent = "2012-01-24 02:30:00 PM";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
Date d1 = null;
Date d0 = null;
try {
d1 = format.parse(sessionTimeStart);
d0 = format.parse(sessionTimerEndOrCurrent);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud) 在普通的Java中,我有这个代码来获取本月的最后一个星期日.
Calendar getNthOfMonth(int n, int day_of_week, int month, int year) {
Calendar compareDate = Date(1, month, year);
compareDate.set(DAY_OF_WEEK, day_of_week);
compareDate.set(DAY_OF_WEEK_IN_MONTH, n);
return compareDate;
}
// Usage
Calendar lastSundayOfNovember = getNthOfMonth(-1, SUNDAY, NOVEMBER, 2012)
Run Code Online (Sandbox Code Playgroud)
什么是干净而优雅的方式来实现相同的结果使用Joda-Time?
我正在尝试解析一个String使用SimpleDateFormat.
这是我目前的代码:
public String getCreatedDateTime() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-ddEHH:mm:ss.zzzz");
try {
Date date = simpleDateFormat.parse("2015-06-27T13:16:37.363Z");
return date.toString();
} catch (ParseException e) {
return "Error parsing date";
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我只是在parse()方法中放置一个常量用于测试目的.
所以,这就是我要解析的内容:
2015-06-27T13:16:37.363Z
这是SimpleDateFormat我使用的模式:
YYYY-MM-ddEHH:MM:ss.zzzz
我一直得到ParseException.
我知道这可能是因为最后的.zzzz,但我不知道.363Z可能代表什么,所以我只是使用了一些随机字母.馊主意.
我会非常感谢你的帮助.谢谢!
我正在阅读Java 7的功能,他们谈到了java.util.Objects课程.
我无法理解的是它之间的功能差异是什么
java.util.Objects.toString(foo)
vs
foo == null ? "":foo.toString()
Run Code Online (Sandbox Code Playgroud)
我只能看到额外的是一个空检查和功能表示法而不是OOP样式.
我错过了什么?
我已经在 IntelliJ IDEA 中设置了 JUnit,并进行了一系列测试,但其中没有任何内容。当我运行它们时,它们都按预期通过。但是,当我输入“assertEquals”时,它显示为红色。当我将鼠标悬停在它上面时,它显示“无法解析方法”。
我用谷歌搜索了一下,看起来我需要这样做:
import static org.junit.Assert.*;
Run Code Online (Sandbox Code Playgroud)
但是,当我开始输入 时import static org.junit.,下一个选项是“*”、“jupiter”或“platform”...
作为参考,我的 IDE 中的示例测试如下所示:
@org.junit.jupiter.api.Test
void isButton() {
assertEquals()
}
Run Code Online (Sandbox Code Playgroud)
知道如何解决这个问题吗?
谢谢!
在任何应用程序中,字符串都会消耗大量内存。每当垃圾收集器访问字符串对象时,它都会记录字符数组。它获取它们的哈希值并将其与对数组的弱引用一起存储。一旦找到另一个具有相同哈希码的字符串,它就会逐个字符地比较它们。如果它们也匹配,一个字符串将被修改并指向第二个字符串的字符数组。然后第一个字符数组不再被引用并且可以被垃圾收集。
字符串池:
java程序使用的所有字符串都存储在这里。如果两个变量被初始化为相同的字符串值。两个字符串不是在内存中创建的,内存中只会存储一个副本,并且都指向同一个内存位置。
因此,java 已经通过检查字符串池中是否存在字符串来处理不在堆中创建重复字符串的问题。那么字符串去重的目的是什么?
如果有如下代码
String myString_1 = new String("Hello World");
String myString_2 = new String("Hello World");
Run Code Online (Sandbox Code Playgroud)
即使它们相同,也会在内存中创建两个字符串。除了字符串重复数据删除有用之外,我想不出任何其他场景。显然我一定错过了一些东西。我错过了什么?
提前致谢
使用Vaadin 8 @PropertyId注释Binder::bindInstanceFields肯定比为每个字段属性绑定编写一行代码更短更甜.
Person person; // `name` is String, `yearOfBirth` is Integer.
…
@PropertyId ( "name" )
final TextField nameField = new TextField ( "Full name:" ); // Bean property.
@PropertyId ( "yearOfBirth" )
final TextField yearOfBirthField = new TextField ( "Year of Birth:" ); // Bean property.
…
// Binding
Binder < Person > binder = new Binder <> ( Person.class );
binder.bindInstanceFields ( this );
binder.setBean ( person );
Run Code Online (Sandbox Code Playgroud)
但是我们得到一个异常抛出,因为yearOfBirth属性是一个整数,这种简单的绑定方法缺少转换器.
严重:
java.lang.IllegalStateException:属性类型'java.lang.Integer'与字段类型'java.lang.String'不匹配.应使用转换器手动配置绑定. …
以秒为单位的纪元时间:1529948813696000
如何将其转换为Java时间戳。
我可以使用这种方法以毫秒为单位转换时代时间
Instant instant = Instant.ofEpochMilli(Long.parseLong("1529957592000"));
Date parsedDate = dateFormat.parse(instant.atZone(ZoneId.of("America/Chicago")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")).toString())
Run Code Online (Sandbox Code Playgroud)
需要帮助将纪元时间转换为微秒吗?
如果我正在使用Java流,并以Unicode字符IntStream的代码点号结尾,那么如何渲染诸如这样的?CharSequenceString
String output = "input_goes_here".codePoints(). ??? ;
Run Code Online (Sandbox Code Playgroud)
我codePoints()在多个接口和类上都找到了一种方法,它们都会生成一个IntStream代码点。但是我还没有找到任何接受相同方法的构造函数或工厂方法。
CharSequence::codePoints() ? IntStreamString::codePoints() ? IntStreamStringBuilder::codePoints() ? IntStream我在寻找相反的东西:
?如何从代码点实例化一个String或CharSequence多个IntStream?
我使用org.openjfx:javafx-archetype-simpleMaven 原型启动了我的第一个 JavaFX 项目。
生成的 POM:
\n\n<project xmlns = "http://maven.apache.org/POM/4.0.0"\n xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"\n xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">\n <modelVersion>4.0.0</modelVersion>\n <groupId>com.example.invoicing</groupId>\n <artifactId>Invoicer</artifactId>\n <version>1.0-SNAPSHOT</version>\n <properties>\n <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>\n <maven.compiler.source>14</maven.compiler.source>\n <maven.compiler.target>14</maven.compiler.target>\n </properties>\n <dependencies>\n <dependency>\n <groupId>org.openjfx</groupId>\n <artifactId>javafx-controls</artifactId>\n <version>14</version>\n </dependency>\n </dependencies>\n <build>\n <plugins>\n <plugin>\n <groupId>org.apache.maven.plugins</groupId>\n <artifactId>maven-compiler-plugin</artifactId>\n <version>3.8.1</version>\n <configuration>\n <release>14</release> ???\n </configuration>\n </plugin>\n <plugin>\n <groupId>org.openjfx</groupId>\n <artifactId>javafx-maven-plugin</artifactId>\n <version>0.0.4</version>\n <configuration>\n <mainClass>com.example.invoicing.App</mainClass>\n </configuration>\n </plugin>\n </plugins>\n </build>\n</project>\nRun Code Online (Sandbox Code Playgroud)\n\n\xe2\x9e\xa5插件的<release>14</release>行的目的是什么?<configuration><artifactId>maven-compiler-plugin</artifactId>
我找到了 Maven 编译器插件的文档Compiling Your Java Sources。但它只提到<!-- put your configurations here -->。所以我对这里的具体配置选项一无所知。 …