我想知道如何从JTextField中获取特定字符.例如,如果JTextField的日期为20/12/2012,那么如何仅从字段中读取"12".
那么它是否可能,如果可能,如何或者更容易使用多个JTextField?
谁能帮我理解为什么我得到不同的月份值
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(dateFormat.format(cal.getTime())
+ "--"
+ cal.get(Calendar.MONTH));
Run Code Online (Sandbox Code Playgroud)
令人惊讶地显示
09/09/2012--8
Run Code Online (Sandbox Code Playgroud) 我进行下一个操作:
将设备上的时区设置为GMT + 0400(不是来自代码)
建立新日期
和打印时间(SimpleDateFormat)-毫秒
得到11-19 19:16:40.920:I / System.out(30070):测试时间:1353330825273时间:19.11
当我在设备上更改我的timeZone到GMT + 0600并现在以毫秒为单位创建日期1353330825273
日期myDate =新日期(1353330825273L);
但是当我再次打印时间时,我得到了11-19 19:17:00.920:I / System.out(30070):测试时间:1353330825273时间:21.11
时间格式字符串为:k:mm
为什么?如何禁用timeZone修改我的时间?
我现在有两种方法.根据场景,当我从数据库中获取数据字段时,它采用BigDecimal格式.所以我决定为它编写一个测试(formatDate()方法).我将BigDecimal传递给方法,但似乎我写了一些错误的代码.从我在示例和SimpleDateFormat API中看到的,我认为我已经正确编写了代码,但我似乎无法弄清楚它抛出一个parseEx会出现什么问题.有人能给我一些提示吗?
private void loadMap() {
//TODO: Uncomment when finished testing.
//DO OTHER STUFF
BigDecimal bd = new BigDecimal(12051998);
System.out.println(formatDate(bd));
}
private String formatDate(BigDecimal bd) {
String value = bd.toString();
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
try {
return format.parse(value);
} catch (ParseException pEx) {
logger.error(pEx);
return "Bad Date Format";
}
}
Run Code Online (Sandbox Code Playgroud)
在此先感谢Warmest,
我试图将格式为dd.MM.YYYY的日期字符串转换为日期对象,如下所示:
String start_dt = "01.01.2011";
DateFormat formatter = new SimpleDateFormat("dd.MM.YYYY");
Date date = (Date)formatter.parse(start_dt);
System.out.print("Date is : "+date);
Run Code Online (Sandbox Code Playgroud)
但我得到了这个结果,Date is : Sun Dec 26 00:00:00 MST 2010
我尝试了不同的ides甚至在compileonline.com上,仍然是相同的结果.所以我在这里做错了什么,因为它不应该表现得像这样.请帮忙.
public static void main(String[] args) {
ParsePosition pp = new ParsePosition(0);
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("America/New_York"));
String datetoparse = "7/1/2003 00:02:53";
Date date = formatter.parse(datetoparse, pp);
System.out.println(date.toString());
date = formatter.parse(datetoparse, pp);
System.out.println(date.toString());
}
Run Code Online (Sandbox Code Playgroud)
格式调用在第一次调用时返回正确的值.但是为什么它在第二次调用时返回null(我在解析与第一次调用中相同的字符串)?
我很困惑为什么这不起作用.无论我改变什么,它打印出同样的东西.这里发生了什么?
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/YYYY");
String s = "11/22/2000";
Date d = sdf.parse(s);
System.out.println(d.toString());
Run Code Online (Sandbox Code Playgroud)
输出: Sun Dec 26 00:00:00 CST 1999
在使用SimpleDateFormat格式化日期时,我得到了正确的日期和月份。但是一年减少了一年。可能是什么问题呢?
public static String getFormattedDate(String date) {
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-mm-dd HH:MM:SS");
Date testDate = null;
String newFormat="space";
try {
testDate = sourceFormat.parse(date);
SimpleDateFormat formatter = new SimpleDateFormat("d/MMM/yyyy");
newFormat = formatter.format(testDate);
Log.i("Formatted Date",newFormat);
}catch(Exception ex){
ex.printStackTrace();
}
return newFormat;
}
Run Code Online (Sandbox Code Playgroud)
实际日期和格式化日期为
01-04 15:34:00.233 21858-21858/com.cube_me.cubeme I/Actual Date: 2016-12-19 00:00:00
01-04 15:34:00.233 21858-21858/com.cube_me.cubeme I/Formatted Date: 19/Dec/2015
Run Code Online (Sandbox Code Playgroud) 基本上,我想设置日期并将其保存到列表中.从下面的代码:
public List<RecordList> createRecord(BigDecimal yr){
for (int index = 0; index <= 14; index++) {
RecordList value = new RecordList();
if(index == 0){
value.setStartDt(toDate(("01-04-" + yr)));
value.setEndDt(toDate(("30-04-" + yr)));
}
if(index == 1){
value.setStartDt(toDate(("01-05-" + yr.add(new BigDecimal(1)))));
value.setEndDt(toDate(("31-05-" + yr.add(new BigDecimal(1)))));
}
createRecord.add(newRecordValue);
return createRecord;
}
private Date toDate(String date) {
Date newDate = null;
try {
newDate = new SimpleDateFormat("dd-MM-YYYY").parse(date);
}
catch (ParseException e) {
e.printStackTrace();
}
return newDate;
}
Run Code Online (Sandbox Code Playgroud)
会发生什么,当我将年份设置为2017年时,输出与我设置的不匹配:
"StartDate": "30-12-2017",
"EndDate": "30-12-2017"
Run Code Online (Sandbox Code Playgroud)
并且它不会增加到明年,而是减少到:
"StartDate": "30-12-2016",
"EndDate": …Run Code Online (Sandbox Code Playgroud) 我写了一段代码来解析日期字符串-
DateFormat cal = new SimpleDateFormat("yyyy-MM-dd hh:mm");
cal.setLenient(false);
try {
cal.parse("2018-01-01 14:42");
}
catch (Exception e)
{
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
但我有一个例外说-
java.text.ParseException: Unparseable date: "2018-01-01 14:42"
at java.base/java.text.DateFormat.parse(DateFormat.java:388)
at MyClass.main(MyClass.java:10)
Run Code Online (Sandbox Code Playgroud)
我不确定为什么我看到此错误,因为日期字符串和给定的格式正确。请帮忙
java ×10
simpledateformat ×10
date ×4
android ×2
calendar ×2
date-parsing ×2
jtextfield ×1
swing ×1
timezone ×1