I have used JCalender to get the date. It returned the date like this
Thu Mar 01 18:35:53 PST 2012
But what I need is this format.
01/03/2012
I have tried the following code:
SimpleDateFormat MydateFormat = new SimpleDateFormat("dd/MM/yyyy");
FromDate.getDateEditor().addPropertyChangeListener(
new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent e) {
if ("date".equals(e.getPropertyName())) {
System.out.println(e.getPropertyName()
+ ": -->" + (Date) e.getNewValue());
try {
fdate = MydateFormat.parse(e.getNewValue().toString());
JOptionPane.showMessageDialog(rootPane, fdate);
} catch (ParseException ex) {
Logger.getLogger(DashboardChart.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
What you were asking for was a new Date object based on the current one. Try this:
package test;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
public class TestDateFormat {
public static void main(String[] args) {
SimpleDateFormat MydateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
String fdate = MydateFormat.format(date);
System.out.println(date + " \tFORMATTED: " + fdate);
JOptionPane.showMessageDialog(null, fdate);
}
}
E.G. Output
Sat Mar 24 23:40:09 EST 2012 FORMATTED: 24/03/2012
Related
Hi i am truing to convert this string 2015-11-26 to Date object so:
I am trying this:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date result=null;
try {
result = df.parse(date);
} catch (ParseException ex) {
Logger.getLogger(XmlReaderDemo.class.getName()).log(Level.SEVERE, null, ex);
}
codecurrency.setDate(result);
And date is the string holding 2015-11-26
It gives me exception and I don't know why.
Your code is working fine:
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.*;
import java.text.ParseException;
public class DateExample {
public static void main (String args[]) {
String date = "2015-11-26";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date result = null;
try {
result = df.parse(date);
} catch (ParseException ex) {
ex.printStackTrace();
}
System.out.println("result: " + result);
}
}
I just changed it a bit, so it compiles.
If you still can't get it to work, please post a Runnable example so we can provide more and better help.
This is the output I get:
result: Thu Nov 26 00:00:00 CST 2015
Also looking at the exception and your code, you probably want to move this line:
codecurrency.setDate(result);
inside the try call...
So I am having this issue that I can't wrap my head around. I've read similar questions posed but very case I've found there is an issue with the format, and my format is correct.
Basically I am trying to convert a String into a Timestamp, and I get the unparseable date error.
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Hello {
public static Timestamp convertStringToTimestamp(String str_date) {
try {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
Date date = formatter.parse(str_date);
java.sql.Timestamp timeStampDate = new Timestamp(date.getTime());
return timeStampDate;
} catch (ParseException e) {
System.out.println("Exception :" + e);
return null;
}
}
public static void main(String[] args) {
Timestamp ts = convertStringToTimestamp("2015-06-09 11:51:12,708");
Timestamp ts2 = convertStringToTimestamp("2015-04-17 11:29:49.564");
System.out.println(ts +" | "+ts2);
}
}
Output:
Exception :java.text.ParseException: Unparseable date: "2015-06-09 11:51:12,708"
Exception :java.text.ParseException: Unparseable date: "2015-04-17 11:29:49.564"
null | null
Any ideas?
This works perfectly to me.
I just passed the right pattern as an input as well.
public static Timestamp convertStringToTimestamp(String str_date, String pattern) {
try {
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
Date date = formatter.parse(str_date);
java.sql.Timestamp timeStampDate = new Timestamp(date.getTime());
return timeStampDate;
} catch (ParseException e) {
System.out.println("Exception :" + e);
return null;
}
}
public static void main(String[] args) {
Timestamp ts = convertStringToTimestamp("2015-06-09 11:51:12,708", "yyyy-MM-dd HH:mm:ss,SSS");
Timestamp ts2 = convertStringToTimestamp("2015-04-17 11:29:49.564", "yyyy-MM-dd HH:mm:ss.SSS");
System.out.println(ts +" | "+ts2);
}
The output is:
2015-06-09 11:51:12.708 | 2015-04-17 11:29:49.564
"2015-06-09 11:51:12,708" is working for me but "2015-04-17 11:29:49.564" won't. You specified the regex for "," so "." would not. It is perfectly normal.
you need to fix the comma
Timestamp ts2 = convertStringToTimestamp("2015-04-17 11:29:49.564");
**Update**
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SomeClass {
public static void main(String[] args) {
System.out.println(convertStringToTimestamp("2015-06-09 11:51:12,708"));
//be consistent here with , and .
System.out.println(convertStringToTimestamp("2015-04-17 11:29:49.564"));
System.out.println();
}
private static Timestamp convertStringToTimestamp(String something) {
SimpleDateFormat dateFormat = null;
if(something.contains(".")) {
dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
}
if(something.contains(",")) {
dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss,SSS");
}
Timestamp timestamp = null;
Date parsedDate;
try {
parsedDate = dateFormat.parse(something);
timestamp = new java.sql.Timestamp(parsedDate.getTime());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return timestamp;
}
}
I am getting an error( Unparseable date: "18–11–2003") when i try to import data from excel file. the date from the file cannot be parsed
if(row.getCell(16)!=null){
String dobb=null;
Date dob=null;
row.getCell(16).setCellType(row.getCell(16).CELL_TYPE_STRING);
dobb=row.getCell(16).getStringCellValue();
System.out.println(dobb);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
try {
dob = (Date)simpleDateFormat.parse(dobb);//error..... Unparseable date: "18–11–2003"
System.out.println("dateeee"+dob);
} catch (ParseException e) {
e.printStackTrace();
//dob=new Date();
}
It looks like the specified "18–11–2003" date contains u2013 Unicode character instead of a normal dash which is u002d.
Here is a sample that uses the string copy-pasted from the question:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDate {
public static void main(String[] args) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
try {
String trouble = "18–11–2003";
String goodOne = "18-11-2003";
Date date = simpleDateFormat.parse(goodOne);
//Date date = simpleDateFormat.parse(trouble);
System.out.println(String.format ("\\u%04x", (int)trouble.charAt(2)));
System.out.println(String.format ("\\u%04x", (int)goodOne.charAt(2)));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
The character in the actual date (–) is not the same character in your date format (-).
This code:
public static void main(String[] args) {
System.out.println((int)'–');
System.out.println((int)'-');
}
produces this result:
8211
45
There are several things you can do:
Do a replace() on your date string (dateStr = dateStr.replace("–", "-");) to replace the strange hyphen with an actual ASCII hyphen. *Recommended*
Change your dateformat from "dd–MM–yyyy" to "dd–MM–yyyy"
I have been playing with calendar, timezones, and conversion since yesterday. This thing has got me all confused. Here is my code:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;
class Dates {
public static void main(String[] args) {
test1();
test2();
}
private static void test1() {
String dateString = "2013-10-06T16:25";
String pattern = "yyyy-MM-dd'T'HH:mm";
SimpleDateFormat dtf = new SimpleDateFormat(pattern);
long mil;
Date date;
try {
date = dtf.parse(dateString);
mil = date.getTime();
System.out.println("Create date:" + date);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(mil);
System.out.println ("millis:"+ mil);
printCal ("Default Cal:", cal);
} catch (ParseException ex) {
Logger.getLogger(Dates.class.getName()).log(Level.SEVERE, null, ex);
}
}
private static void test2() {
// TODO Auto-generated method stub
long ms = 1381091100000L;
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Americas/New_York"));
cal.setTimeInMillis(ms);
printCal ("EST: ", cal);
}
private static void printCal(String str, Calendar cal) {
// TODO Auto-generated method stub
System.out.println(str+"Year:" + cal.get(Calendar.YEAR) + ", Month:"
+ cal.get(Calendar.MONTH) + ", Date:"
+ cal.get(Calendar.DATE) + ", Hour:"
+ cal.get(Calendar.HOUR) + ", Minutes:"
+ cal.get(Calendar.MINUTE) + ", Seconds:"
+ cal.get(Calendar.SECOND) + ", AM_PM: +"+ cal.get(Calendar.AM));
}
}
The output:
Create date:Sun Oct 06 16:25:00 EDT 2013
millis:1381091100000
Default Cal:Year:2013, Month:9, Date:6, Hour:4, Minutes:25, Seconds:0, AM_PM: +1
EST: Year:2013, Month:9, Date:6, Hour:8, Minutes:25, Seconds:0, AM_PM: +1
As you can see, I am using the same millis in test2() that I get from the test1(). The Hour in test 1 is the expected 4 while in test2 it is 8! What is causing this problem?
Thanks.
Your "Default Cal" prints time in your local timezone: EDT (-4)
Your "EST" calendar prints time in UTC/GMT timezone, because "Americas/New_York" is incorrect (extra "s"), it should be "America/New_York", since it's incorrect, TimeZone.getTimeZone(...) returns UTC
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have two programs but I cannot find a way to merge the two of them together. Is there anyway I can do this?
This is the fisrt part of code by converting the date to a string
package main_program;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class testing2 {
public static void main( String[] args) {
SimpleDateFormat format = new SimpleDateFormat(" EEE MMM dd HH:mm:ss zzz yyyy ");
String time = " Wed Dec 14 00:00:00 CST 2012 ";
Date date = null;
try {
date = format.parse(time);
} catch (ParseException e1) {
e1.printStackTrace();
}
try {
System.out.println("Format To times:");
System.out.println(date.getTime());
}catch (Exception e){
e.printStackTrace();
}
}
}
This is the second part of the code by converting the string in long to a formated date
package main_program;
import java.sql.Timestamp;
public class lta {
public static void main(String[] args) {
java.util.Date ABC= new java.util.Date(1355241600000l);
System.out.println(new Timestamp(ABC.getTime()));
}
}
package main_program;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.sql.Timestamp;
public class testing2 {
public static void main( String[] args) {
SimpleDateFormat format = new SimpleDateFormat(" EEE MMM dd HH:mm:ss zzz yyyy ");
String time = " Wed Dec 14 00:00:00 CST 2012 ";
Date date = null;
try {
date = format.parse(time);
} catch (ParseException e1) {
e1.printStackTrace();
}
try {
System.out.println("Format To times:");
System.out.println(date.getTime());
Timestamp ts = new Timestamp(date.getTime());
System.out.println(ts);
}catch (Exception e){
e.printStackTrace();
}
}
}
To merge them together, you could instead of defining two main methods in separate classes, create a single larger class with the given methods defined. The methods should probably be set to return values, rather than printing, in that case. You could print the values returned from them, if you so desire. Something like this:
Modified to accomplish the goal stated in comments:
public class MyClass {
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.sql.Timestamp;
public static void main(String[] args) {
//Do whatever you are intending to, calling 'lta' and 'testing2', like:
String time=" Wed Dec 14 00:00:00 CST 2012 ";
long longtime = testing2(time);
System.out.println(lta(longtime));
}
public static Timestamp lta(long time)
{
java.util.Date ABC= new java.util.Date(time);
return new Timestamp(ABC.getTime());
}
public static long testing2(String time) {
SimpleDateFormat format = new SimpleDateFormat( " EEE MMM dd HH:mm:ss zzz yyyy " );
Date date = null;
try {
date = format.parse(time);
} catch (ParseException e1) {
e1.printStackTrace();
return null;
}
try {
return String.valueOf(date.getTime());
}catch (Exception e){
e.printStackTrace();
return null;
}
}
}