我正在添加这个问题,因为我是Java和Android的新手,我搜索了几个小时试图解决这个问题.答案来自相关答案的组合,所以我想我会记录我为其他可能正在努力的人学到的东西.见答案.
对于一些背景知识,我的经验主要是PHP的Web开发和一点Ruby.我唯一的操作系统是Linux(Ubuntu Studio),我(不情愿地)在Android Studio 2.1.2中开发我的第一个Android应用程序.我的Java设置如下:
>java -version
> openjdk version "1.8.0_91"
> OpenJDK Runtime Environment (build 1.8.0_91-8u91-b14-3ubuntu1~15.10.1-b14)
> OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)
Run Code Online (Sandbox Code Playgroud) datetime android android-gradle-plugin threetenbp threetenabp
我只是想在Java 8中将日期字符串转换为DateTime对象.运行以下行:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDateTime dt = LocalDateTime.parse("20140218", formatter);
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Exception in thread "main" java.time.format.DateTimeParseException:
Text '20140218' could not be parsed:
Unable to obtain LocalDateTime from TemporalAccessor:
{},ISO resolved to 2014-02-18 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
Run Code Online (Sandbox Code Playgroud)
语法与此处的建议相同,但我遇到了一个例外.我在用JDK-8u25.
我有一个LocalDate需要获得该月的第一天和最后一天.我怎么做?
例如.13/2/2014我需要以LocalDate格式获得1/2/2014和28/2/2014.
使用threeten LocalDate类.
有人可以告诉我,以下两个陈述之间的区别是什么:
TimeZone.getTimeZone("America/New_York")
Run Code Online (Sandbox Code Playgroud)
和
TimeZone.getTimeZone("EST")
Run Code Online (Sandbox Code Playgroud)
换句话说,为什么EST与America/New_York不同.同样在我的应用程序中,要获得美国当前时区,我应该使用America/New_York还是EST.
我需要在我的项目中从一个时区转换到另一个时区.
我可以从我当前的时区转换到另一个时区,但不能从不同的时区转换到另一个时区.
例如,我在印度,我能够使用Date d=new Date();并将其分配给日历对象并设置时区,从印度转换为美国.
但是,我不能从不同的时区到另一个时区这样做.例如,我在印度,但我无法将时区从美国转换到英国.
我需要在Java中解析日期的RFC 2822字符串表示.这里有一个示例字符串:
星期六,2010年3月13日11:29:05 -0800
它看起来很讨厌,所以我想确保我做的一切都正确,并且会在以后通过AM-PM /军事时间问题,UTC时间问题,我没有预料到的问题等解释错误而遇到奇怪的问题等等. ...
谢谢!
如何 在Date对象中解析此日期字符串2013-03-13T20:59:31 + 0000?
我尝试过这种方式,但不工作.
DateFormat df = new SimpleDateFormat("YYYY-MM-DDThh:mm:ssTZD");
Date result = df.parse(time);
Run Code Online (Sandbox Code Playgroud) 当我尝试将字符串解析为日历时,我收到了最奇怪的错误.它似乎弄乱了我用来设置结果日历时间的Date对象.错误非常不一致(或者我看不到任何逻辑).谁能指出我可能做错了什么?
public class caltest{
public static final SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss.SSS");
public static void main(String[] args) {
String date1 = "1992-03-11 12:00:12.123";
String date2 = "1993-03-11 12:00:12.123";
String date3 = "1994-03-11 12:00:12.123";
String date4 = "1995-03-11 12:00:12.123";
parseStringAsCalendar(date1);
parseStringAsCalendar(date2);
parseStringAsCalendar(date3);
parseStringAsCalendar(date4);
}
public static String calendarToString(Calendar cal) {
return sdf.format(cal.getTime());
}
public static Calendar parseStringAsCalendar(String s) {
Date time = null;
try {
time = sdf.parse(s);
} catch (ParseException e) {
System.out.println("Exception");
e.printStackTrace();
}
System.out.println(time.toString());
GregorianCalendar ret = …Run Code Online (Sandbox Code Playgroud) 这是我的下面函数,我在其中传递时间戳,我只需要从时间戳返回的日期而不是小时和秒.以下代码我得到 -
private String toDate(long timestamp) {
Date date = new Date (timestamp * 1000);
return DateFormat.getInstance().format(date).toString();
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的输出.
11/4/01 11:27 PM
Run Code Online (Sandbox Code Playgroud)
但我只需要这样的日期
2001-11-04
Run Code Online (Sandbox Code Playgroud)
有什么建议?
我正在将一些代码从使用 Java 8 转换LocalDatetime为使用该版本kotlinx-datetime,但我找不到任何格式化方法。具体来说,我正在替换FormatStyle.MEDIUM. 它们不存在并且我需要编写格式吗?
这是针对 Android 应用程序的。有没有我可以使用的 Android 特定库?或者我可以使用 Java 8 之前的方法来保持对旧版本 Android 的支持吗?
编辑(我的解决方案基于 Arvind 的答案):
fun Instant.toDateTimeString(formatStyle: FormatStyle = FormatStyle.MEDIUM): String {
val localDatetime = toLocalDateTime(TimeZone.currentSystemDefault())
val formatter = DateTimeFormatter.ofLocalizedDateTime(formatStyle)
return formatter.format(localDatetime.toJavaLocalDateTime())
}
Run Code Online (Sandbox Code Playgroud)