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.
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:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 3 years ago.
Hi I am not able to understand what time format we need to use in order to parse this date2020-02-11T17:26:31-05:00 I have tried using Date formatter and simple date format but its not working
Date is coming in this form ->2020-02-11T17:26:31-05:00 I am not able to identify the type of this date
Below is snippet of code i have tried but its throwing exception
DateTimeFormatter responseFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss-SSSXXX'Z'",
Locale.ENGLISH);
responseDateTime = LocalDateTime.parse(otmmResponseDate, responseFormatter);
Notice that your date string has an offset -05:00 in it. Thus, your string does not represent a LocalDateTime, but an OffsetDateTime, and should be parsed by OffsetDateTime.parse (not everything is a LocalDateTime!):
// the format is ISO 8601, so it can be parsed directly without a DateTimeFormatter
OffsetDateTime odt = OffsetDateTime.parse("2020-02-11T17:26:31-05:00");
If you only want the local date time part of it, then you can call toLocalDateTime afterwards:
LocalDateTime ldt = odt.toLocalDateTime();
This is a datetime String that contains an offset of minus five hours. You don't even have to use a DateTimeFormatter directly, because parsing this to an OffsetDateTime will do:
public static void main(String[] args) {
String dateString = "2020-02-11T17:26:31-05:00";
OffsetDateTime odt = OffsetDateTime.parse(dateString);
System.out.println(odt.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
}
Output:
2020-02-11T17:26:31
Not that this uses a DateTimeFormatter without an offset for the output.
This question already has answers here:
Java: Check the date format of current string is according to required format or not [duplicate]
(8 answers)
Closed 6 years ago.
I have a scenario where I get the date value from the xml and the date value from the website. I am able to retrieve the results, But now I need to compare the format of the retrieved results whether it is in 'MM YYYY' format or not ? How can i do that ? i have to check the format in String manner ie for e.g Apr 2010 but not 04 2010
Please can anyone of you help me ??
public class SimpleDate {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
String strDate = sdf.format(date);
sdf = new SimpleDateFormat("MMM/yyyy");
strDate = sdf.format(date);
System.out.println(strDate);
}
}
You can use a regular expresson, like this (Jan|Feb|Mar...) d{4}
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.
I have a date string called allianceStartDate which has the value of
"1/7/2010"
I am trying to convert this date string to util Date object. The code which I have tried is as follows:
Date d = new SimpleDateFormat("dd/mm/yyyy").parse(allianceStartDate);
However the result of this operation is:
Fri Jan 01 00:07:00 GMT 2010
The desired result is a Date object in the format: "01/07/2010".
Thanks for any help offered.
It should be "dd/MM/yyyy" not "dd/mm/yyyy", We use mm for minutes not for Month in Java. You should use MM for month.
Read more about Java SimleDateFormat.
After parsing your initial text to obtain the Date object you will need to format it usig also a date formater in order to display it as a formatted text. Here is an exemple:
SimpleDateFormat SIMPLE_DATE_FORMATTER = new SimpleDateFormat("dd/MM/yyyy");
public String toSimpleDateFormat(Date d) {
return SIMPLE_DATE_FORMATER.format(d);
}