Tag: 简化

使用Java静态方法中的“today”和“yesterday”字符串简化日期对象的替换

我有以下方法,我想更短或更快,如果没有别的。 欢迎所有评论: Bellow方法接受一个日期对象,形成它(“EEE hh:mma MMM d,yyyy”),然后确定日期是今天还是昨天,如果是,则返回“(昨天|今天)hh:mma “格式化的字符串。 public static String formatToYesterdayOrToday(String date) { SimpleDateFormat sdf = new SimpleDateFormat(“EEE hh:mma MMM d, yyyy”); Date in = null; try { in = sdf.parse(date); } catch (ParseException e) { log.debug(“Date parsing error:”, e); } Calendar x = Calendar.getInstance(); x.setTime(in); String hour = Integer.toString(x.get(Calendar.HOUR)); String minute = Integer.toString(x.get(Calendar.MINUTE)); String […]

简化Java中的分数

我的任务是发展理性课程。 如果500和1000是我的输入,则(½)必须是我的输出。 我自己写了一个程序来找到它。 还有另一种找到解决方案的最佳方法,或者我的程序已经是最好的了吗? public class Rational { public static void main(String[] args){ int n1 = Integer.parseInt(args[0]); int n2 = Integer.parseInt(args[1]); int temp1 = n1; int temp2 = n2; while (n1 != n2){ if(n1 > n2) n1 = n1 – n2; else n2 = n2 – n1; } int n3 = temp1 / n1 ; […]