2012年12月1日00:00:00 GMT
我必须将上述日期转换为以下格式
2012年12月1日
我怎么能够?
我试过以下方法,但它没有工作
public Date ConvertDate(Date date){
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String s = df.format(date);
String result = s;
try {
date=df.parse(result);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}
Run Code Online (Sandbox Code Playgroud) 我试过了
int year = Calendar.get(Calendar.YEAR);
Run Code Online (Sandbox Code Playgroud)
但它给了我编译时错误
无法从静态上下文引用非静态方法'get(int)'.
我从observable的调用方法调用此方法.
Observable.combineLatest(ob1 ob2,
ob3, new Func3<String, String, String, Boolean>() {
@Override
public Boolean call(String a, String b, String c) {...
Run Code Online (Sandbox Code Playgroud)
我也见过(new Date()).getYear();但它被弃用了.
我一直在开发使用此代码的Android应用程序:
Date d=new Date(new Date().getTime()+28800000);
String s=new SimpleDateFormat("dd/MM/yyyy hh:mm:ss").format(d);
Run Code Online (Sandbox Code Playgroud)
我需要在当前时刻8小时后得到日期,我希望这个日期有24小时格式,但我不知道如何通过SimpleDateFormat来实现它.我还需要日期DD/MM/YYYY HH:MM:SS格式.
我有一个类似下面的值Mon Jun 18 00:00:00 IST 2012,我想将其转换为18/06/2012
怎么转换这个?
我尝试过这种方法
public String toDate(Date date) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date theDate = null;
//String in = date + "/" + month + "/" + year;
try {
theDate = dateFormat.parse(date.toString());
System.out.println("Date parsed = " + dateFormat.format(theDate));
} catch (ParseException e) {
e.printStackTrace();
}
return dateFormat.format(theDate);
}
Run Code Online (Sandbox Code Playgroud)
但它抛出以下异常:
java.text.ParseException: Unparseable date: "Mon Jun 18 00:00:00 IST 2012"
我想解析一个使用特定时区创建的日期,将其转换为格式并返回.转换有效,但时区偏移始终设置为+0000,并根据需要添加/减去时差.如何使其格式化并保持偏移正确?
我期待这个:2012-11-30T12:08:56.23 + 07:00
但得到这个:2012-11-30T05:08:56.23 + 00:00
执行:
public static final String ISO_8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSZZ";
public static String formatDateToISO8601Standard(Date date) {
DateTime dateTime = new DateTime(date);
DateTimeFormatter df = DateTimeFormat.forPattern(ISO_8601_DATE_FORMAT);
return dateTime.toString(df);
}
Run Code Online (Sandbox Code Playgroud)
测试类:
private static final String DATE_WITH_TIMEZONE = "30 11 2012 12:08:56.235 +0700";
private static final String EXPECTED_DATE_WITH_TIMEZONE = "2012-11-30T12:08:56.23+07:00";
@Test public void testFormattingDateWithSpecificTimezone() throws Exception {
String result = JodaDateUtil.formatDateToISO8601Standard(createDate(DATE_WITH_TIMEZONE));
assertEquals("The date was not converted correctly", EXPECTED_DATE_WITH_TIMEZONE, result); }
private Date createDate(String dateToParse) throws ParseException {
DateTimeFormatter …Run Code Online (Sandbox Code Playgroud) 为了显示可重现的场景,我正在做以下事情
获取当前系统时间(当地时间)
将本地时间转换为UTC //在这里工作正常
反转UTC时间,返回当地时间.遵循3种不同的方法(如下所列),但所有3种方法仅保留UTC时间.
{
long ts = System.currentTimeMillis();
Date localTime = new Date(ts);
String format = "yyyy/MM/dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat (format);
// Convert Local Time to UTC (Works Fine)
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date gmtTime = new Date(sdf.format(localTime));
System.out.println("Local:" + localTime.toString() + "," + localTime.getTime() + " --> UTC time:" + gmtTime.toString() + "-" + gmtTime.getTime());
// Reverse Convert UTC Time to Locale time (Doesn't work) Approach 1
sdf.setTimeZone(TimeZone.getDefault());
localTime = new Date(sdf.format(gmtTime));
System.out.println("Local:" + localTime.toString() + "," …Run Code Online (Sandbox Code Playgroud)java utc simpledateformat date-formatting timestamp-with-timezone
这就是我现在所拥有的
Seconds = (60 - timeInMilliSeconds / 1000 % 60);
Minutes = (60 - ((timeInMilliSeconds / 1000) / 60) %60);
Run Code Online (Sandbox Code Playgroud)
我认为是正确的.几小时和几天它应该像 -
Hours = ((((timeInMilliSeconds / 1000) / 60) / 60) % 24);
Days = ((((timeInMilliSeconds / 1000) / 60) / 60) / 24) % 24;
Run Code Online (Sandbox Code Playgroud)
然后-
TextView.SetText("Time left:" + Days + ":" + Hours + ":" + Minutes + ":" + Seconds);
Run Code Online (Sandbox Code Playgroud)
但我的时间和日子都是不正确的
我过去几个小时一直在网上搜索,以获得系统时区的日期时间.
当我使用calendar.getTimezone.getDefaultName时,它总是返回GMT.(理想情况下它应该返回我当前的时区,即IST)我正在尝试将此字符串"2014-02-14T06:04:00:00"转换为我的时区日期时间.它总是在GMT中同时返回我.
我只看到每个人都建议使用时区.即
dateFormatter.setTimezone("any_arbitary_timezone");
Run Code Online (Sandbox Code Playgroud)
Point是我的应用程序将用于不同的地理位置.我无法将其设置为特定时区.它应设置为系统时区,以便它可以显示在用户当前所在的任何时区
我想转换String myDate = "2014/10/29 18:10:45"成
long ms (i.e. currentinmlilies)?我在Google上寻找它,但我只能找到如何将ms转换为日期.
注意:为了说清楚,我想从1970/1/1格式的日期获得ms.
如果给我一些随机日期,那么我需要检查该日期是否确实存在,然后显示true或false.我需要返回false两个日期32-11-2010, 31-02-2010因为2月份不包含31日.我们如何用Java做到这一点?