- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在构建一个 Java 方法,使用 JodaTime 库对两个时间段进行算术求和。我的代码工作正常,但我认为可以优化以减少执行时间......不幸的是,我是 JodaTime 的新手。
这是我的代码:
import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.joda.time.format.PeriodFormatter;
import org.joda.time.format.PeriodFormatterBuilder;
public class Test {
public static void main(String[] args)
{
String time1 = "08:00";
String time2 = "08:00";
System.out.println(Operation_Sum(time1,time2));
}
private static String Operation_Sum(String time1, String time2)
{
String output;
long start = System.currentTimeMillis();
if (time1.equals("00:00") && time2.equals("00:00"))
{
output = "00:00";
}
else if (time1.equals("00:00"))
{
output = time2;
}
else if (time2.equals("00:00"))
{
output = time1;
}
else
{
boolean sign_time1 = false;
boolean sign_time2 = false;
String[] time1_out, time2_out;
boolean negative = false;
String output_split[];
if (time1.contains("-"))
{
time1_out = time1.split(":");
time1_out[0] = time1_out[0].replace("-", "");
time1_out[1] = time1_out[1].replace("-", "");
time1 = time1_out[0] + ':' + time1_out[1];
sign_time1 = true;
}
if (time2.contains("-"))
{
time2_out = time2.split(":");
time2_out[0] = time2_out[0].replace("-", "");
time2_out[1] = time2_out[1].replace("-", "");
time2 = time2_out[0] + ':' + time2_out[1];
sign_time2 = true;
}
PeriodFormatterBuilder builder = new PeriodFormatterBuilder();
builder.minimumPrintedDigits(2);
builder.printZeroAlways();
builder.appendHours();
builder.appendLiteral(":");
builder.appendMinutes();
PeriodFormatter pf = builder.toFormatter();
Period period1 = pf.parsePeriod(time1);
Period period2 = pf.parsePeriod(time2);
Period normalized;
Period total = null;
if (sign_time1 && sign_time2)
{
total = period1.plus(period2);
negative = true;
}
if (sign_time1 && !sign_time2)
{
total = period2.minus(period1);
}
if (!sign_time1 && sign_time2)
{
total = period1.minus(period2);
}
if (!sign_time1 && !sign_time2)
{
total = period1.plus(period2);
negative = false;
}
normalized = total.normalizedStandard(PeriodType.time());
output_split = pf.print(normalized).split(":");
if (output_split[1].contains("-"))
{
output = output_split[0] + ":" + output_split[1].replace("-", "");
}
else
{
output = (negative ? "-" : "") + output_split[0] + ":" + output_split[1];
}
}
long end = System.currentTimeMillis();
System.out.println("exec time sum1: " +(end - start) + " ms");
return output;
}
}
谢谢!! :)
最佳答案
该代码确实非常需要 CR。
如果你想让它更快,就先让它更短。 JVM 有点拒绝优化过长的方法。
关于优化,看起来您在字符串上浪费了大部分时间。不管怎样,清理代码,然后分析,然后优化是正确的顺序。
从你的程序来看,你在做什么并不完全清楚,但是,它不可能真的正确。正在做
time1_out[0] = time1_out[0].replace("-", "");
表示您将 --12:--34---
转换为 12:34
。这是故意的吗?如果不是,哪里允许破折号?如果仅在第一个位置(例如 -12:34
),那么 time1_out.substring(1)
肯定更干净、更快。等等...
I'm using two strings because the input comes from a database
是否有任何数据库不能存储日期或数字?如果 12:34
表示大约十二点半,则将其存储为 DATE
。如果表示 12 小时 34 分钟或 12 分 34 秒,则将其存储为 INTEGER。如果有更好的数据类型,永远不要操作字符串。
关于java - 我该如何优化这段代码? (需要 JodaTime),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25683624/
我正在使用 JodaTime弄清楚当前是什么LocalDate是然后获得下一个星期一的日期。 当我使用以下方法并且当前日期是星期一时,它不是获取下一个星期一,而是获取当前日期。 private Loc
我不在乎它是 MST 还是 MDT,但我有一个带有时间的 LocalDateTime 对象(即没有时区)。我想添加 MST 或 MDT 并创建一个 DateTime 对象,以便我可以获得毫秒,因为存储
我有一个非常简单的 Spark 程序(在 Clojure 中使用 Flambo,但应该很容易理解)。这些都是 JVM 上的对象。我正在测试 local实例(尽管我猜测 Spark 仍然会序列化和反序列
这个问题已经有答案了: Is there a class in java.time comparable to the Joda-Time Interval? (4 个回答) 已关闭 7 年前。 Ja
我正在尝试http://www.epochconverter.com/我可以做 100 年,但是当我转到 99 年时,它似乎崩溃并报告 1999 而不是 0099。这是一个错误吗?纪元时间应该能够代表
我正在匹配诸如此类的事件 [Sun Jan 11 10:43:35 2015][3205.51466981] user idp : testing 10.234.22.220 (10.234.22.2
我在Hibernate 4中将jadira用户类型用于joda时间。我需要使用jvm日期时间进行存储和检索。问题是回读的日期有2个小时的偏移量。我们在UTC + 2上。日期已正确存储在数据库中。数据库
我正在尝试计算 Android 应用程序中任意两个给定时间之间的差异。为完成此任务,我使用了 JodaTime 和 Period 类以及 Period 的 getYears()、getDays()、g
我有这个 sql 查询和 Java 代码: select to_char(start_date, 'DD/MM/YYYY') AS start_date FROM table LogS
我想使用以下 MDX 语句: with member [x] as now()->plusMonths(1)->withDayOfMonth(1)->minusDay(1) select [x] on
我的应用程序正在使用 JodaTime 来管理日期解析和格式设置。 我有这个时间戳:2018-07-24T15:30:00-07:00。如何将其显示为下午 3:30,而不管用户身在何处? 最佳答案 以
这个问题已经有答案了: Find total hours between two Dates (11 个回答) 已关闭 3 年前。 我计算两个日期之间的时间差。一个来自数据库,另一个来自当前时间。 D
目前我有一个函数可以获取一天的开始时间和结束时间,并计算两者之间的差异,从而给出一天的工作时间。我想做的是能够获取 7 天的工作时间,并返回总计,同时保留显示格式 (HH:mm)。 我的单日总计函数:
当我从字符串中检索日期时,例如使用 Jodatime 格式化类的“Sat 11 Feb”: DateTimeFormatter formatter = DateTimeFormat.forPatter
我有一个让我很困惑的问题。首先,我将讲述我对从特定类实例化对象的了解。 我们通过声明类型(也可能是接口(interface)类型)、引用名称(变量)来创建类的实例(或对象),在等于运算符的另一侧我们使
我希望能够为下一次出现的时间获取正确的DateTime,我只需指定一小时和一分钟即可。我还希望能够通过在该小时和分钟指定相同的事情来做到这一点,但在(例如)星期三发生下一次发生的事情。 另外,请注意,
我得到了非常直接的DateConverter方法,可以将String转换为org.joda.time.DateTime public class DateConverter { private
与 JodaTime DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(Da
这会引发无效格式异常: DateTimeFormat.forPattern("%E, %d %M %Y %H:%m:%s %z").parseMillis("Sun, 20 May 2012 01:0
请问我想计算两个日期之间的时间段,我搜索了但没有找到我需要的。我有这个代码: public int getNbjours() { DateTime dt1 = new DateTime
我是一名优秀的程序员,十分优秀!