When I parsing time in java, I passing "12:12" as string argument, then I am getting "Thu Jan 01 12:12:00 IST 1970" as a output.
I want current year like "Fri Mar 09 12:12:00 IST 2012" as a output.
String timestr = "12:12";
Date convertedDate = null;
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
convertedDate = dateFormat.parse(timestr);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(convertedDate);
Thanks!
I think that the problem with this is that you are creating a date with null values and then just initialize the time value. I think you should use the Calendar class and get an instance of the Calendar and then set the time. Once that is done, you create a date object from the Calendar and parse it to your needs.
Related
Basically I'm trying to convert string data to Timestamp format. When I converted data to timestamp format, SimpleDateFormat added three minutes.
Because milisecond data equals 3 minute.But I want to preserve milisecond data on timestamp value.
Code:
public Double TimestampTest(String Timestamp ){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date parsedDate = null;
try {
parsedDate = dateFormat.parse(Timestamp);
} catch (ParseException e1) {
e1.printStackTrace();
}
Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
...
}
Timestamp value
2002-04-17 23:45:58.983
For test case
System.out.println(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse("2015-04-22T19:54:11.219983Z"));
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS'Z'").parse("2015-04-22 19:54:11.219983Z"));
Results are same for both of them
Wed Apr 22 19:57:50 EEST 2015
Wed Apr 22 19:57:50 EEST 2015
Because
219 983 milliseconds = 3.66638333 minutes
To sum up I want to preserve ms data .Is there any way to do it ?
If I understand, what you have at the end of your string are microseconds, not milliseconds. The old, obsolete Date and SimpleDateFormat classes have no support for microseconds. But you can use the new java.time classes:
System.out.println(ZonedDateTime.parse("2015-04-22T19:54:11.219983Z",
DateTimeFormatter.ofPattern(("yyyy-MM-dd'T'HH:mm:ss.SSSSSSVV"))));
I need to convert the string 531772200000 to a Java Date object. The date is stored in a database.
When I am doing this, I am getting java.text.ParseException: Unparseable date: "531772200000".
My code:
String dateToBeConverted = String.valueOf(dbObject.get("customerDateOfBirth"));
String parseabledate = dateToBeConverted
.replace("/Date(","")
.replace(")/","")
.replace("+0530", "");
dbObject.put("_id", String.valueOf(dbObject.get("userInfoId")));
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
Date date;
date = formatter.parse(parseabledate);
This looks like a timestamp value, this will probably give you the date:
new Date(Long.parseLong("531772200000"));
which works out at Fri Nov 07 1986 18:30:00 GMT+0000
Here is one solution that will provide the date correctly formatted.
String d = "531772200000";
SimpleDateFormat newFormatter = new SimpleDateFormat("dd-MMM-yyyy");
try {
Date date1 = new Date(Long.parseLong(d));
System.out.println(newFormatter.format(date1)); //Will print out as 07-Nov-1986
} catch (ParseException e) {
e.printStackTrace();
}
Another solution is to use Joda Time with a solution below.
String d = "531772200000";
DateTime newDate = new DateTime(Long.parseLong(d));
DateTimeFormatter dd = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
DateTime date = dd.parseDateTime(newDate.toString());
System.out.println(date.toString("dd-MMM-yyyy")); //Prints out as 07-Nov-1986
Personally I prefer to use the second solution (Joda Time) as it is much nicer and easier.
It is epoh time format.
Somebody already answered about that in https://stackoverflow.com/a/20732668/379779
and test your epoh time in this website http://www.epochconverter.com/
I am developing an Android app.
In this app I am working with dates.
I need all dates to be in UTC format. I am using this method to convert them:
public Date getConvertedUTCDate(String datetime) {
Date myDate = null;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
try {
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
myDate = dateFormat.parse(datetime);
} catch (ParseException e) {
e.printStackTrace();
}
return myDate;
}
But instead of getting the expected:
Sun Aug 10 14:13:14 UTC 2014
I get it in CEST format
Sun Aug 10 14:13:14 CEST 2014
What am I doing wrong?
You only set the TimeZone for the calendar of the parser. Not the TimeZone of the parsed result.
I have a TimeStamp '2013-06-24 10:46:11.0' and I need to cut off the .0 part, so what I did was to use the SimpleDateFormat to parse it to String and back then parse it to date, the first conversion was fine but the second (string to date) throws a java date time.
public void convert(Object object) {
Date date;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date = object().getDate();
String formated = format.format(date);
try {
date = format.parse(formated);
} catch (ParseException ex) {
Logger.getLogger(DlgConsultaFactura.class.getName()).log(Level.SEVERE, null, ex);
}
}
What I expect is a date like this 2013-06-24 10:46:11, but what I got is this date Mon Jun 24 10:46:11 CDT 2013
Any help will be appreciated. Thanks.
Mon Jun 24 10:46:11 CDT 2013 and 2013-06-24 10:46:11 is actually same value. Mon Jun 24 10:46:11 CDT 2013 is as per your default locale.
You're getting confused between date's internal representation and its display format.
To print in 2013-06-24 10:46:11 you can use same SimpleDateFormat object again.
You can use DateFormat#format(Date) to print the date or return the String representation in your desired format i.e. "yyyy-MM-dd HH:mm:ss". Something like this:
String myDt = format.format(date);
// 2013-06-24 10:46:11
Not the best way but a quick and easy one if you want just the string representation of the date ...
formated = formated.substring(0, formated.length()-2);
:)
DateFormat i.e. SimpleDateFormat just formats the date as per your need.
Parse returns the Date representation of the String (or timestamp in your case) passed in.
Format returns the String representation of the Date object passed in.
In both the cases , you see the same date just the representation is different.
I am trying to format a date by parsing it and then formating it but it is not working.
It is showing a parsing exception
public java.util.Date convertFormat(String DateTimeForm)
throws ParseException {
DateTimeForm="2012-06-01 10:00 PM";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aaa");
java.util.Date FCDate = (java.util.Date) formatter.parse(DateTimeForm);
return (java.util.Date) FCDate;
}
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aaa");
try {
Date date = formatter.parse("2012-06-01 10:00 PM");
System.out.println(date.toString());
} catch (ParseException e) {
e.printStackTrace();
}
Didn't change anything and yet it works.
Fri Jun 01 22:00:00 CDT 2012
This works fine on my machine. I didn't change anything important.
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aaa");
Date date = null;
try {
date = formatter.parse("2012-06-01 10:00 PM");
} catch (ParseException ex) {
// Intentionally empty. Failed parse causes date == null.
}
System.out.print(date);
prints
Fri Jun 01 22:00:00 EDT 2012
The Java docs say the numerics are all locale-independent, but not the AM/PM. For example the code fails if you specify Locale.JAPAN in the formatter construction. Specify Local.US to guarantee AM/PM will always work.