This question already has answers here:
Calendar date to yyyy-MM-dd format in java
(11 answers)
Closed 7 years ago.
I need to parse the date as per format but it didn't work very well.
public class Main {
public static void main(String[] args) throws ParseException {
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "07/06/2013";
Date date = formatter.parse(dateInString);
System.out.println(date);
}
}
i need date object in 07/06/2013 format even if date was in any format. but parse method always return in Fri Jun 07 00:00:00 PKT 2013.
You can always have your date object in "dd/MM/yyyy" format - when you want to output it just use:
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(formatter.format(date));
The reason you see Fri Jun 07 00:00:00 PKT 2013 displayed is because System.out.println uses the default toString representation of the object you provided. For Date instances it gives you such information (depending on locale, afaik).
P.S. keep in mind that instances of SimpleDateFormat are not thread-safe so it is better to create new ones.
Related
This question already has answers here:
parsing date/time to localtimezone
(2 answers)
How to Parse Date from GMT TimeZone to IST TimeZone and Vice Versa in android
(4 answers)
SimpleDateFormat returns wrong time zone during parse
(2 answers)
Java SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") gives timezone as IST
(9 answers)
Closed 2 years ago.
I have a String which represents a Date in the UTC timezone (because my database uses UTC). I want to convert this String into a date with SimpleDateFormat. The problem is that converts it into a Date in the CEST timezone without adding the 2 hour separating UTC and CEST. Here is the code:
//This is a date in UTC
String text = "2020-09-24T09:45:22.806Z";
//Here I define the correct format (The final Z means that it's UTC)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
//Then I parse it
Date date = sdf.parse(text);
//Then I print it
System.out.println(date);
The result of the print is
Thu Sep 24 09:45:22 CEST 2020
Why CEST? I would like it to remain UTC, but if it has to become CEST at least add the 2 hours
You should setTimeZone() to your DateFormat like
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) throws ParseException {
//This is a date in UTC
String text = "2020-09-24T09:45:22.806Z";
//Here I define the correct format (The final Z means that it's UTC)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
//Then I parse it
Date date = sdf.parse(text);
//Then I print it
System.out.println(date);
}
}
I also replaced 'Z' to X following the documentation
This question already has answers here:
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
display Java.util.Date in a specific format
(11 answers)
Closed 2 years ago.
I need to convert the current timestamp and current minus 1-minute timestamp into a given format. I was able to convert the desired format. But it was returning as String.
I need the formatted output as Date instead of String.
public static void main(String[] args) throws ParseException
{
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
long now = System.currentTimeMillis();
String toTimeStamp = dateFormatter.format(now);
long nowMinus1Minutes = now - TimeUnit.MINUTES.toMillis(1);
String fromTimeStamp = dateFormatter.format(nowMinus1Minutes);
System.out.println(fromTimeStamp);
System.out.println(toTimeStamp);
// Tried with parse
Date fromDate = dateFormatter.parse(fromTimeStamp);
Date toDate = dateFormatter.parse(toTimeStamp);
System.out.println(fromDate);
System.out.println(toDate);
}
Output:(String)
2020-07-24 12:13:00
2020-07-24 12:12:00
Output: Pose Parsed the string as Date
Fri Jul 24 12:16:00 IST 2020
Fri Jul 24 12:17:00 IST 2020
Expected Output:
2020-07-24 12:13:00 (As Date Object)
2020-07-24 12:12:00 (As Date Object)
Can't be possible. Understood lately. Closing this.
Once you transform your String into a Date, the initial format you used, whatever it is is lost.
If you want to display your data in the "yyyy-MM-dd HH:mm:00", use your formatter again otherwise Date class will stick to its default format:
System.out.println(formatter.format(fromDate));
System.out.println(formatter.format(toDate));
If you need two Date-Object, you could do it like this:
OffsetDateTime offsetDateTime1 = OffsetDateTime.now();
OffsetDateTime offsetDateTime2 = offsetDateTime1.minusMinutes(1);
System.out.println(Date.from(offsetDateTime1.toInstant()));
System.out.println(Date.from(offsetDateTime2.toInstant()));
If you need the timestamp to be printed in other format, you can use SimpleDateFormat on the Date-Objects afterwards.
public static void main(String[] args) throws ParseException {
SimpleDateFormat dt = new SimpleDateFormat("MM dd yy");
dt.setLenient(false);
dt.setTimeZone(TimeZone.getTimeZone("Asia/Hong_Kong"));
Date date = dt.parse("05 14 16");
System.out.println(date);
}
Output: Fri May 13 21:30:00 IST 2016
If i try to use the output it is switching to one day before instead of the correct day.
Is this expected or an issue with the API?
This is expected and there is no bug in Java.
Class Date does not contain timezone information. A java.util.Date is nothing more than wrapper for a number of milliseconds since 01-01-1970, 00:00:00 GMT. It does not remember that the string that it was parsed from contained information about a timezone.
When you display a Date, for example by (implicitly) calling toString() on it as you are doing here:
System.out.println(date);
it will be printed in the default timezone of your system, which is IST in your case.
If you want to print it in a certain timezone, then format it using a SimpleDateFormat object, setting the desired timezone on the SimpleDateFormat object. For example:
DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss Z");
df..setTimeZone(TimeZone.getTimeZone("Asia/Hong_Kong"));
System.out.println(df.format(date));
This question already has answers here:
Date Format JAVA
(5 answers)
Closed 9 years ago.
I have a date in the following format
//input date
Thu Jun 06 2013 00:00:00 GMT+0530 (India Standard Time)
//output date format
I want to change this to "dd-mm-yyyy, hh:mm:ss".
I get the input date format from db. I have to change that into output date format which i will be showing it in a grid.
I tried the following code.
DateFormat outputDate = new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss");
try
{
Date date = outputDate.parse(facade.getDate.toString()); **//getting exception here**
outputDate = new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss");
Date date1 = new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss").parse(outputDate
.format(date));
facade.setDate(date1);
}catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
I am getting
java.text.ParseException: Unparseable date: "2013-06-06 00:00:00.0".
Any help..
"2013-06-06 00:00:00.0" does not match "dd-mm-yyyy, hh:mm:ss" your format should be "dd-MM-yyyy hh:mm:ss" instead
But, looking at your code I'm guessing facade.getDate is actually a java.sql.Timestamp which inherits from java.util.Date so you can directly pass it to the format like so
new SimpleDateFormat("dd-MM-yyyy, hh:mm:ss").format(facade.getDate)
Here's some code which works for me:
import java.text.*;
import java.util.*;
public class Test {
public static void main(String[] args) throws Exception {
String input = "Thu Jun 06 2013 00:00:00 GMT+0530 (India Standard Time)";
DateFormat inputFormat = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss 'GMT'z",
Locale.ENGLISH);
Date date = inputFormat.parse(input);
DateFormat outputFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss",
Locale.ENGLISH);
outputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String output = outputFormat.format(date);
System.out.println(output);
}
}
Things to consider:
You need to work out your output time zone. Currently I've got it set to UTC, but that may not be what you want.
You really need to take a step back and think things through. You've clearly got two different formats - you're trying to convert from one to the other. So creating three different SimpleDateFormat objects all with the same format is never going to work.
You need to read documentation carefully... in SimpleDateFormat, M means month and m means minute; h uses the 12-hour clock and H uses the 24-hour clock.
This is assuming you actually need to start with a string though. If getDate is already a Date or a Timestamp, you can ignore the first part - just use the output part of the above code. You should avoid unnecessary string conversions wherever possible.
Note that dd-MM-yyyy is a slightly unusual format - are you sure you don't actually want yyyy-MM-dd which is more common (and sortable)?
DateFormat outputDate = new SimpleDateFormat("yyyy-dd-mm hh:mm:ss");
try {
Date date = outputDate.parse("2013-06-06 00:00:00.0");
System.out.println(new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss").format(date));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
works well, line 1 was incorrect. Your SimpleDateFormat.parse needs to be in the exact format of the input date. Then you want to output it in a different format so you make another one and set the format then call SimpleDateFormat.format(date) and I put a println on it.
Fault is here
DateFormat outputDate = new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss");
pattern should be equals to Thu Jun 06 2013 00:00:00 GMT+0530 (India Standard Time). not to your out put strings pattern.
#Test
public void test() throws ParseException {
SimpleDateFormat sdf_org = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss 'GMT'Z", Locale.ENGLISH);
Date d = sdf_org.parse("Thu Jun 06 2013 00:00:00 GMT+0530");
SimpleDateFormat sdf_target = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss.SSS");
System.out.println(sdf_target.format(d));
}
output console : 2013-30-06 03:30:00.000
I used SimpleDateFormat:
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
value.adStartDate = df.parse("2011/11/11 11:11:11");
I was hoping the date would come out like the string I provided, but instead I am getting this:
Fri Nov 11 2011 11:11:11 GMT-0500 (Eastern Standard Time)
This is showing on a form that I created using Javascript...
Is there a way to "force" the output on the form to be like the string?
Basically I want to pass a date with format "yyyy/MM/dd hh:mm:ss" to a form that was generated using Javascript and have the form display it in that same format.
One thing is parsing and another thing is formatting.
Check this example in order to display the formatted string.
#Test
public void testDateFormat() throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
Date myDate = df.parse("2011/11/11 11:11:11");
System.out.println(df.format(myDate));
}
Output:
2011/11/11 11:11:11
What do you want to achieve? You have successfully parsed "2011/11/11 11:11:11" String into java.util.Date object. Date.toString() yields the string you see (Fri Nov 11 2011 11:11:11 GMT-0500 (Eastern Standard Time)).
If you now want to format the Date object back to String, use df.format() which does the opposite thing compared to df.parse().