今天是2010年2月9日星期二,当我打印日期时,我收到了错误的日期:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date today = formatter.parse(String.format("%04d-%02d-%02d",
Calendar.getInstance().get(Calendar.YEAR),
Calendar.getInstance().get(Calendar.MONTH),
Calendar.getInstance().get(Calendar.DAY_OF_MONTH)));
System.out.println("Today is " + today.toString());
Run Code Online (Sandbox Code Playgroud)
打印行导致:"今天是2010年1月9日00:00:00 CST 2010"
它肯定不是09年1月9日星期六,也就是2月9日星期二.我假设我做错了什么,所以有人能让我知道这里有什么问题吗?我是否必须手动设置星期几?
更新
注意:我今天不想初始化,new Date()因为我希望初始化为小时,分钟,秒和毫秒0.这是必要的,所以我可以将用户输入日期与今天进行比较:如果用户输入今天的日期并且我使用格式化程序来构造Date对象,那么如果我今天初始化new Date()并且我比较两个日期 - 今天将在用户之后选定日期(不正确).因此,我需要在没有hr/min/sec/ms的情况下在今天开始时进行初始化.
我有以下代码来格式化日期:
def currentDate = new Date().format('E, dd MMM yyyy')
Run Code Online (Sandbox Code Playgroud)
格式如我所料,但它是用我的电脑语言编写的.我怎样才能用英文给出日期?有帮助吗?谢谢!
我有一个显示当前时间的开始按钮,我希望能够有一个停止时间比当前时间晚一个小时的按钮.我该怎么做呢?
这是显示当前时间的按钮的代码
Button stopButton = (Button) this
.findViewById(R.id.StartTrackingEditStopTime_button);
// using SimpleDateFormat class
SimpleDateFormat sdfStopTime = new SimpleDateFormat("hh:mm:ss a",
Locale.ENGLISH);
String newStoptime = sdfStopTime
.format(new Date(System.currentTimeMillis()));
stopButton.append(newStoptime);
Run Code Online (Sandbox Code Playgroud)
非常感谢您提供有关如何执行此操作的任何帮助或建议.
我String从一个Date对象得到这个:Mon Mar 25 00:00:00 IST 2013
String日期格式的表示是什么?
使用以下SimpleDateFormat时:
SimpleDateFormat format = new SimpleDateFormat("hh:mm");
Run Code Online (Sandbox Code Playgroud)
然后我解析然后比较2个值:12:19并11:40像这样:
val = format.parse("12:19").compareTo(format.parse("11:40"));
Run Code Online (Sandbox Code Playgroud)
我val成为-1,这似乎是错误的,因为12小时和19分钟大于11小时和40分钟
但是,当我将第一个值更改为19:19并再次比较它11:40然后val返回值1似乎是正确的.
不知道为什么会这样,我想我错过了什么.
我想测试一个修复,其中SimpleDateFormat用作静态类变量.我得到了
以前我的代码是
public class Abc{
private static SimpleDateFormat dateformatter;
public static String method1(final Calendar calendar) {
String thePattern = "ddMMM";
dateformatter = new SimpleDateFormat(thePattern, Locale.US);
sPars = dateformatter.format(calendar.getTime());
//Something
}
public static String method2(final Calendar calendar) {
String thePattern = "ddMMyyyy";
dateformatter = new SimpleDateFormat(thePattern, Locale.US);
sPars = dateformatter.format(calendar.getTime());
//Something
}
}
Run Code Online (Sandbox Code Playgroud)
对于这个,我得到了以下异常
java.lang.ArrayIndexOutOfBoundsException: 965
at sun.util.calendar.BaseCalendar.getCalendarDateFromFixedDate(BaseCalendar.java:454)
at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2333)
at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2248)
at java.util.Calendar.setTimeInMillis(Calendar.java:1140)
at java.util.Calendar.setTime(Calendar.java:1106)
at java.text.SimpleDateFormat.format(SimpleDateFormat.java:955)
at java.text.SimpleDateFormat.format(SimpleDateFormat.java:948)
at java.text.DateFormat.format(DateFormat.java:336)
Run Code Online (Sandbox Code Playgroud)
现在我已将其更改为:
public class Abc{
public static String method1(final Calendar …Run Code Online (Sandbox Code Playgroud) 我把日期作为字符串
String dateStr = Mon Mar 31 2014 00:00:00 GMT+0530 (India Standard Time)
Run Code Online (Sandbox Code Playgroud)
但是当使用SimpleDateFormat进行解析时,我得到了不可解决的日期异常
java.util.Date date = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss Z", Locale.ENGLISH).parse(dateStr);
Run Code Online (Sandbox Code Playgroud)
请帮我解决这个问题
我需要在Java中将字符串数组转换为Date类型。我查了一些示例代码,它们几乎都要求做同样简单的事情。这就是我在做什么:
String[] dateString = { "2014/05/01", "2014/05/02", "2014/05/03", "2014/05/04", "2014/05/05"};
Date[] dt = new Date[5];
for(int i=0;i<count;i++){
dt[i]= new SimpleDateFormat("yyyy/mm/dd").parse(dateString[i]);
}
Run Code Online (Sandbox Code Playgroud)
问题出在循环内的行上,我收到一条错误消息,指出:“ ParseException未处理的异常”。Eclipse建议我用try and catch块将其包围,这是我做的。它现在可以运行,但是我在dt数组中输入的日期与我输入的日期不匹配。我想我正在获取某种默认值,该值从2014年1月1日开始。
有人知道这是什么以及如何解决?
谢谢!
我用它来获取时间戳
long epoch = new java.text.SimpleDateFormat ("dd/MM/yyyy HH:mm:ss").parse("09/22/2008 16:33:00").getTime();
Run Code Online (Sandbox Code Playgroud)
它返回989929568.
然后我使用在线转换器将此数字更改回标准时间,
我得到的是
05 / 15 / 01 @ 12:26:08pm
Run Code Online (Sandbox Code Playgroud)
不是
09/22/2008 16:33:00
Run Code Online (Sandbox Code Playgroud)
怎么了?
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class myTests {
public static void main(String[] args) {
DateFormat formatter = new SimpleDateFormat("YYYY-MMM-dd", Locale.US);
try {
Date sortingDate = (Date)formatter.parse("2017-Jul-13");
System.out.println("Sorted Date is:"+sortingDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果是
排序日期为:2017年1月1日00:00:00太平洋标准时间
为什么不显示我给2017年7月13日的日期你可以告诉我.
谢谢安倍晋三