我期望来自以下方面的例外:
LocalDate.parse("9/31/2018",DateTimeFormatter.ofPattern("M/d/yyyy"));
Run Code Online (Sandbox Code Playgroud)
但是相反,我得到了2018年9月30日!?别误会我的意思,它很聪明很不错,但是我已经期望Java的Date类的精度会更高……
谁能阐明为什么/为什么?这将弄乱我的测试。
我试图得到我当前的系统日期,并将其格式化为Etc/UTC,然后进入这个"dd.MM.yyyy HH:mm z"格式.问题是格式化日期后格式化函数返回一个字符串而不是日期.这是下面的代码spinet
final Date currentDate = new Date();
final SimpleDateFormat dateFormatter = new SimpleDateFormat("dd.MM.yyyy HH:mm z");
dateFormatter.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String finalDateTime= dateFormatter.format(currentDate);
System.out.println(finalDateTime);
Run Code Online (Sandbox Code Playgroud)
是否有任何替代解决方案允许我通过将我保留在此日期对象中来格式化日期,因为我已经在java 8之后和java 8之前研究了每个日期库,看起来如果我想格式化任何类型的日期或日期时间,我必须使用格式化程序将给定日期转换为字符串.
任何替代解决方案或不可能吗?
我正在将字符串转换为日期格式年份和日期显示正确但月份显示不同.
String s = "2018-08-29"
try
{
DateFormat formatter = new SimpleDateFormat("yyyy-MM-DD", Locale.ENGLISH);
Date parse = formatter.parse(s);
long time = parse.getTime();
}catch (ParseException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
输出:1月29日00:00:00 GMT + 2018:05 2018
我使用class Calendar创建Date。但是在我的测试案例中,当我将日期设置为31/12或30/12时,日期的年份是我设置的日期年份+ 1.示例2018-12-29-> 2018-12-29但2018-12-30 -> 2019年12月30日。我不知道为什么?我的代码:
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class MyClass {
public static void main(String args[]) {
calendarBug(2018, 11, 29);
calendarBug(2018, 11, 30);
}
public static void calendarBug(int year, int month, int day) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
Date date = calendar.getTime();
System.out.println(new SimpleDateFormat("YYYY-MM-dd").format(date));
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
2018-12-29
2019-12-30
Run Code Online (Sandbox Code Playgroud) 这可能是重复的,但我无法弄清为什么当将MMM指定为MMM并与mm(数字)一起使用时,该月返回零。这里的任何帮助将不胜感激?
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.Locale;
public class time1 {
public static void main(String[] args) {
DateFormat originalFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat("yyyy-mm-dd");
Date date = null;
try {
date = originalFormat.parse("26-Aug-2011");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String formattedDate = targetFormat.format(date);
System.out.println("old date: " + date);
System.out.println("new date: " + formattedDate);
}
}
Run Code Online (Sandbox Code Playgroud)
输出为:
旧日期:IST 2011年8月26日星期五00:00:00
新日期:2011 -00- 26年
当格式更改为dd-mm-yyyy且日期为26-08-2011时,输出为
旧日期:IST 2011年1月26日星期三00:07:00 …
我在尝试获取DateUtils.getRelativeTimeSpanString以返回其在Android中应该遇到的问题时遇到了严重的问题。
我在这里经历过每个类似的问题,而发布的解决方案都没有起作用或没有意义。
这是我的代码:
CharSequence relativeDate =
DateUtils.getRelativeTimeSpanString(System.currentTimeMillis(),
currentNewsItem.getTimeInMilliseconds(),
DateUtils.HOUR_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE);
dateView.setText(relativeDate);
Run Code Online (Sandbox Code Playgroud)
currentNewsItem.getTimeInMilliseconds方法确实确实返回了很长的时间(例如,刚过去1521759734它看起来正确),并且getRelativeTimeSpanString将正确格式的日期字符串以“ 2018年3月23日”的格式传递给视图,因此它知道正确的日期。但是我需要它通过“ x小时前”或“ x分钟前”,而且我无法终生弄清楚为什么不是这样。
我已经检查了模拟器上的时间,并且问题出现在我的设备上,因此不是时区设置。
文档说,如果时间跨度大于7天,它将恢复为这样的日期字符串,但是我检查了过去的时间,因为旧时间已经过去,转换器网站显示这是4小时前的确切时间。此外,它显示的是正确的日期(今天),而不是小时/分钟前。
我已经尝试过将HOUR_IN_MILLIS,MINUTES_IN_MILLIS和SECONDS_IN_MILLIS常量用于第三个参数,并且尝试删除了最后一个缩写参数,但结果仍然相同。
我尝试了各种公共方法签名来尝试查找可能有效的签名,但它们都具有相同的结果。
有没有人遇到过这种情况,或者有人可以指出我要去哪里错了?非常感谢。
android android-date apache-commons-dateutils android-dateutils java-date
java 6.0
假设今天是: 2018年8月8日
这里方法返回2个日期的差异,以月为单位.
public static Integer getDiffMonths(Date dateFrom, Date dateTo) {
Calendar cal1 = new GregorianCalendar();
cal1.setTime(dateFrom);
Calendar cal2 = new GregorianCalendar();
cal2.setTime(dateTo);
return differenceInMonths(cal1, cal2);
}
private static Integer differenceInMonths(Calendar dateFrom, Calendar dateTo) {
int month1 = dateFrom.get(Calendar.YEAR) * 12 + dateFrom.get(Calendar.MONTH);
int month2 = dateTo.get(Calendar.YEAR) * 12 + dateTo.get(Calendar.MONTH);
int diff = month2 - month1;
return diff;
}
Run Code Online (Sandbox Code Playgroud)
这里有4个测试用例的结果:
1.
currentDate = Aug 08 2018
DateTo = Aug 13 2018
diffMonth = 0
Run Code Online (Sandbox Code Playgroud)
2.
currentDate = …Run Code Online (Sandbox Code Playgroud) 在我的帮助器类中,我从日志文件中提取键字符串.在那里我搜索一个日期和子串的文本.问题是当我包括该日期包括使用java日期的短语时,硬编码的日期以不同的方式起作用.
代码段:
BufferedReader br = new BufferedReader(new FileReader("/developer.log"));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
line = br.readLine();
}
String second = sb.toString();
String pattern = "yyyy-MM-dd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = "]"+simpleDateFormat.format(new Date());
Matcher m2 = Pattern.compile("^(.*)date(.*)$").matcher(second);
if (m2.find()) {
String keyPrefix = "Bearer ";
key = keyPrefix + m2.group(1);
}
}
br.close();
return key;
Run Code Online (Sandbox Code Playgroud)
该date变量不会返回模式匹配,但是当我对字符串进行硬编码时]2019-03-01它会起作用.这可能是什么问题?提前致谢.
我有三个变量需要使用本年度的年份,即从现在开始的一年之前和从现在开始的两年之前,使用Java。这样的事情会起作用吗?
String DNRCurrentYear = new SimpleDateFormat("yyyy").format(new Date());
Run Code Online (Sandbox Code Playgroud)
还是我需要将年份定int为减去一年然后减去两年?
我将如何获得当年减去一年,当年减去两年?
尝试运行此代码,直到一切正常为止,OCT但是NOV就像
firstdate 2019-12-01 & lastdate 2020-12-31
public class Test1 {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, 1);
String date;
calendar.set(Calendar.DATE,calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
Date nextMonthFirstDay = calendar.getTime();
date=new SimpleDateFormat("YYYY-MM-dd").format(nextMonthFirstDay).toLowerCase();
System.out.println("firstdate "+ date);
calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
Date nextMonthLastDay = calendar.getTime();
date=new SimpleDateFormat("YYYY-MM-dd").format(nextMonthLastDay).toLowerCase();
System.out.println("lastdate "+date);
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么会这样显示。是Java中的错误还是bug?
从有效载荷我试图以HH:mm:ss格式发送时间.我想以毫秒为单位返回.
下面我能够用它来转换日期(在SO的帮助下).
String myDate = "2018/11/13 12:00:00";
LocalDateTime localDateTime = LocalDateTime.parse(myDate, DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss") );
long millis = localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
System.out.println(millis);
Run Code Online (Sandbox Code Playgroud)
我需要将HH:mm:ss转换为毫秒
如果我在12:30:00通过,我需要毫秒......毫秒毫秒
java-date ×11
java ×10
datetime ×3
android ×2
android-date ×1
date ×1
date-parsing ×1
java-8 ×1
matcher ×1