Java 雄辩地印刷;“之间的时间”;两次打印
我有一个消息传递系统,我有一个Java 雄辩地印刷;“之间的时间”;两次打印,java,java-8,Java,Java 8,我有一个消息传递系统,我有一个消息类的(非书面)方法,我希望能够返回一个有说服力的字符串,表示两点之间最显著的时间差: Message(String sender, String channel, String message, String target) { this.sender = sender; this.channel = channel; this.message = message; this.target = target; this.t
消息
类的(非书面)方法,我希望能够返回一个有说服力的字符串,表示两点之间最显著的时间差:
Message(String sender, String channel, String message, String target) {
this.sender = sender;
this.channel = channel;
this.message = message;
this.target = target;
this.timestamp = System.nanoTime();
}
private String getTime() {
long now = System.nanoTime();
return "";
}
public String toString() {
return String.format("(%s ago)[%s] %s => %s", this.getTime(), this.channel, this.sender, this.message);
}
我不希望对不同的值循环使用时间单位
枚举,而得到最大的两个非零值。有没有更简单的方法来检索两点之间的用户友好时间?Java8的新时代API为此引入了什么
请记住,我不想要一个特定的日期,而是想要两个日期的差异。当你说“我不想循环使用不同值的时间单位枚举”时,我想你的意思是“我希望其他人为我做这件事”,因为我认为没有其他方法
您可以做的是依赖于或者,取决于您的用例中的持续时间。您可以要求这些类为您生成字符串,它们将只包含有效单位,如果格式不适合您,可以使用regexp或其他格式进行转换
例如:
private String getTime() {
long now = System.nanoTime();
Duration d = Duration.ofNanos(now - this.timestamp);
String durStr = d.toString()
.replaceFirst("PT", "")
.replaceFirst("H", " hours ")
.replaceFirst("M", " minutes ")
.replaceFirst("S", " seconds");
return durStr;
}
如果持续时间可以从很短到很长,您可能需要将这两个类结合起来。但是在这一点上,自己编写代码可能同样容易…java 8中新java.time包的示例代码使用
该库提供类来帮助构建字符串。但是我在java.time中没有看到类似的解决方案。因此,在根本不使用任何外部库的情况下,我成功地构建了一个有点粗糙/冗长的解决方案。它完美无瑕,但并不意味着我不会讨厌它:)
private String getTime(){
long diff=System.nanoTime()-this.timestamp;
TimePoint=this.getTimePoint(diff);
if(point.getNext()!=null){
返回String.format(“%d%s,%d%s”,point.getTime(),point.getUnit().toString(),
point.getNext().getTime(),point.getNext().getUnit().toString());
}否则{
返回String.format(“%d%s”、point.getTime()、point.getUnit().toString());
}
}
私有静态类时间点{
私人最终很长时间;
私人最终计时单位;
下一个私人时间点;
公共时间点(长时间、时间单位、下一个时间点){
这个时间=时间;
这个。单位=单位;
this.next=next;
}
公共长getTime(){
这次回来;
}
公共时间单位getUnit(){
返回此.unit;
}
公共时间点getNext(){
把这个还给我,下一个;
}
公共时间点设置下一个(时间点下一个){
this.next=next;
归还这个;
}
}
/**
*检索最大非零次的绝对恶心的方法
*纳秒周期内的差异。原谅我的罪
*
*@自1.0.0以来
*@version 1.0.0
*
*@param diff两点之间以纳秒为单位的差值
*@return一个新的{@link TimePoint}表示所表示的最大时间
*/
私有时间点getTimePoint(长差异){
如果(差异<0){
返回null;
}
长温;
时间单位u=时间单位。纳秒;
时间点根=空;
如果((温度=u.toDays(diff))>0){
root=新的时间点(temp,TimeUnit.DAYS,null);
diff-=root.getUnit().toNanos(root.getTime());
}
如果((temp=u.toHours(diff))>0 | |(root!=null&&temp>=0)){
时间点p=新时间点(温度、时间单位.HOURS、空);
root=this.allocateNodes(root,p);
diff-=p.getUnit().toNanos(p.getTime());
}
如果((temp=u.toMinutes(diff))>0 | |(root!=null&&temp>=0)){
时间点p=新的时间点(温度、时间单位、分钟、空);
root=this.allocateNodes(root,p);
diff-=p.getUnit().toNanos(p.getTime());
}
如果((temp=u.toSeconds(diff))>0 | |(root!=null&&temp>=0)){
时间点p=新的时间点(温度,时间单位。秒,空);
root=this.allocateNodes(root,p);
diff-=p.getUnit().toNanos(p.getTime());
}
如果((temp=u.toMillis(diff))>0 | |(root!=null&&temp>=0)){
TimePoint p=新的时间点(temp,TimeUnit.millizes,null);
root=this.allocateNodes(root,p);
diff-=p.getUnit().toNanos(p.getTime());
}
如果((temp=u.toMicros(diff))>0 | |(root!=null&&temp>=0)){
时间点p=新时间点(温度,时间单位为微秒,空);
root=this.allocateNodes(root,p);
diff-=p.getUnit().toNanos(p.getTime());
}
if(diff>=0 | |(root!=null&&temp>=0)){
时间点p=新时间点(温度,时间单位。纳秒,空);
root=this.allocateNodes(root,p);
diff-=p.getUnit().toNanos(p.getTime());
}
返回根;
}
专用时间点分配节点(时间点根,时间点分配){
if(root==null){
收益分配;
}else if(root.getNext()!=null){
返回root.setNext(this.allocateNodes(root.getNext(),allocate));
}否则{
返回root.setNext(分配);
}
}
工作原理:
时间点是表示时间量的单个点,以及传递的时间单位
。它还有一个getNext
,它将返回一个设置的下一个值。布尔检查root!=null&&temp>=0
,这样,如果已经有根节点,它将只添加0的值(这样整个时间点对象链将读取“1分钟、0秒、19毫秒”等)
allocateNodes
只是递归地跟踪一系列TimePoint
s,直到它可以找到一个空节点,并将其设置在那里
如果没有
时间单位有类似于的东西,我真的想不出任何可行的方法来做到这一点而不把它完全写出来,但是这一部分现在已经完成了。你说的“雄辩的字符串”和“用户友好的时间”是什么意思?你是说
Period p = Period.between(birthday, today);
long p2 = ChronoUnit.DAYS.between(birthday, today);
System.out.println("You are " + p.getYears() + " years, " + p.getMonths() +
" months, and " + p.getDays() +
" days old. (" + p2 + " days total)");
private String getTime() {
long diff = System.nanoTime() - this.timestamp;
TimePoint point = this.getTimePoint(diff);
if (point.getNext() != null) {
return String.format("%d %s, %d %s", point.getTime(), point.getUnit().toString(),
point.getNext().getTime(), point.getNext().getUnit().toString());
} else {
return String.format("%d %s", point.getTime(), point.getUnit().toString());
}
}
private static class TimePoint {
private final long time;
private final TimeUnit unit;
private TimePoint next;
public TimePoint(long time, TimeUnit unit, TimePoint next) {
this.time = time;
this.unit = unit;
this.next = next;
}
public long getTime() {
return this.time;
}
public TimeUnit getUnit() {
return this.unit;
}
public TimePoint getNext() {
return this.next;
}
public TimePoint setNext(TimePoint next) {
this.next = next;
return this;
}
}
/**
* Absolutely disgusting method of retrieving the largest non-zero times
* difference in a nanosecond period. Forgive me for my sins
*
* @since 1.0.0
* @version 1.0.0
*
* @param diff The difference in nanoseconds between two points
* @return A new {@link TimePoint} representing the largest represented time
*/
private TimePoint getTimePoint(long diff) {
if (diff < 0) {
return null;
}
long temp;
TimeUnit u = TimeUnit.NANOSECONDS;
TimePoint root = null;
if ((temp = u.toDays(diff)) > 0) {
root = new TimePoint(temp, TimeUnit.DAYS, null);
diff -= root.getUnit().toNanos(root.getTime());
}
if ((temp = u.toHours(diff)) > 0 || (root != null && temp >= 0)) {
TimePoint p = new TimePoint(temp, TimeUnit.HOURS, null);
root = this.allocateNodes(root, p);
diff -= p.getUnit().toNanos(p.getTime());
}
if ((temp = u.toMinutes(diff)) > 0 || (root != null && temp >= 0)) {
TimePoint p = new TimePoint(temp, TimeUnit.MINUTES, null);
root = this.allocateNodes(root, p);
diff -= p.getUnit().toNanos(p.getTime());
}
if ((temp = u.toSeconds(diff)) > 0 || (root != null && temp >= 0)) {
TimePoint p = new TimePoint(temp, TimeUnit.SECONDS, null);
root = this.allocateNodes(root, p);
diff -= p.getUnit().toNanos(p.getTime());
}
if ((temp = u.toMillis(diff)) > 0 || (root != null && temp >= 0)) {
TimePoint p = new TimePoint(temp, TimeUnit.MILLISECONDS, null);
root = this.allocateNodes(root, p);
diff -= p.getUnit().toNanos(p.getTime());
}
if ((temp = u.toMicros(diff)) > 0 || (root != null && temp >= 0)) {
TimePoint p = new TimePoint(temp, TimeUnit.MICROSECONDS, null);
root = this.allocateNodes(root, p);
diff -= p.getUnit().toNanos(p.getTime());
}
if (diff >= 0 || (root != null && temp >= 0)) {
TimePoint p = new TimePoint(temp, TimeUnit.NANOSECONDS, null);
root = this.allocateNodes(root, p);
diff -= p.getUnit().toNanos(p.getTime());
}
return root;
}
private TimePoint allocateNodes(TimePoint root, TimePoint allocate) {
if (root == null) {
return allocate;
} else if (root.getNext() != null) {
return root.setNext(this.allocateNodes(root.getNext(), allocate));
} else {
return root.setNext(allocate);
}
}