I could not find an alternative toDateTimeAtStartOfTheDay. For example
DateTime.now().toLocalDate().toDateTimeAtStartOfDay().plusHours(10)
how would I write above code in Java 8's DateTime library?
Closest I came to ZonedDateTime.now().toLocalDate().atStartOfDay() which just prints 2015-07-21T00:00.
I want something like 2015-07-21T00:00:00.000-04:00
If you need the time as a formatted String and you always like to get 10 o'clock of today, then don't bother calculating that time manually and write it into a format pattern:
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T10:00:00.000'XXX");
The meaning of each letter can be found here: JavaDoc of DateTimeFormatter. 'T10:00:00.000' is a fixed string and won't be parsed, just "added" to the returned String.
You can get the formatted time like this:
ZonedDateTime.now().format(format);
The output would be:
2015-07-21T10:00:00.000-04:00
You can use:
LocalDate.now().atStartOfDay().atZone(ZoneId.systemDefault());
Related
I tried searching across the web, but unable to find a suitable answer and hence posting here.
I am calling another API which gives me the date-time like
"2022-02-05T17:13:20-06:00[America/Chicago]"
I would like to convert this to a format like
"2022-02-05T17:13:20.000Z" (I am unsure what the milli-second will turn out as)
Could someone help me achieve this?
I am unable to get any example for this specific conversion scenario!!!
Regards,
Sriram
For the sample values given getting to a UTC timestamp should be something like
ZonedDateTime.parse("2022-02-05T17:13:20-06:00[America/Chicago]").toInstant().toString()
The datetime value including the timezone is the canonical representation of a ZonedDateTime and therefore can be parsed as such.
Instant is a UTC timestamp that prints in the form of 2022-02-05T23:13:20Z
You could take more influence on the behavior using a DateTimeFormatter - but since both input and output seem to be standard formats it does not seem necessary.
Here below is a code snippet that I was able to generate....
But, I am unsure of that is correct. Please let me know if you feel anything is incorrect.
String requestTime = "2022-02-05T17:13:20-06:00[America/Chicago]";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX'['VV']'");
ZonedDateTime zonedDateTime = ZonedDateTime.parse(requestTime, formatter);
zonedDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"));
System.out.println(zonedDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")));
public void display() {
System.out.printf("%-10d%-12s%-12s%10.2f%8d%12.2f%1$td.%1$tm.%1$ty %n\n",
this.getID(),
this.getFirstName(),
this.getLastName(),
this.getState(),
this.getNo(),
this.getAmt(),
this.getDate());
}
Is my method for printing out some of my info. this.getDate returns date as a Date (java.util.Date) type.
I had an example given to me as System.out.printf("%1$td.%1$tm.%1$ty %n", date); to print out the data in a dd.mm.yyyy format. I tried to place that into my code, but by the looks of things I made a formatting mistake?
I've been trying a few printing methods now but find it confusing how it would work on its own but then I have issues when I try to place it into a larger printf statement.
The issue is that the number before the $-sign indicates the absolute index (starting with 1! 😉) of the Object. You intended to point at the Date (Index 7) but pointed at the first element instead, which happend to be an Integer. You'll want to use "%-10d%-12s%-12s%10.2f%8d%12.2f%7$td.%7$tm.%7$ty %n\n" for formatting.
First of all Date formatting is a complex issue and is already dealt with for you. So you trying to come up your own formatter (such as "%-10d%-12s%-12s%10.2f%8d%12.2f%td.%toString() of class Date. If you need better formatting than look at method DateFormat.format(Date date). However, note that if you use Java 8 or higher than the class java.util.Date shouldn't be used. Look at java.time package. Use ZonedDateTime or any of its "brothers" instead of Date and use DateTimeFormatter to format it to string
I want to define a pattern for the Java SimpleDaterFormat to parse existing strings.
The existing dates look like this: 2011-05-02T13:40:00+02:00.
I tried with different patterns, but I got ParseExceptions. The problem seems to be the timezone format.
Printing the pattern in Java:
yyyy-MM-dd'T'HH:mm:ssZ
2012-03-14T15:40:44+0100
yyyy-MM-dd'T'HH:mm:ssz
2012-03-14T15:41:58MEZ
But how can I get
???
2011-05-02T13:40:00+02:00
I'm using Java 6, not Java 7.
If you can use Java 7 or newer, you can use the XXX pattern to get the timezone to look like +02:00:
yyyy-MM-dd'T'HH:mm:ssXXX
Otherwise you might have to manipulate the date string to remove the colon from the timezone before parsing it.
I know it's a bit old question, but someone else might benefit from my hint.
You can use JodaTime. As library documentation stands:
Zone: 'Z' outputs offset without a colon, 'ZZ' outputs the offset with
a colon, 'ZZZ' or more outputs the zone id.
You can use it as well with java 6. You have more examples in this question
In database, i have timestamp values like
2005-JAN-13 07:15:31.22222
I want to format above value in java to
2005-05-13 07:15:31.22222
fot this i used formetter: yyyy-MM-dd HH:mm:ss:SSSSS
but when i use above formetter , it is giving value as follows:
2005-05-13 07:15:31:00222
instead of 2005-05-13 07:15:31.22222
can any one pls suggest java formater to get the value as follows:
2005-05-13 07:15:31.22222
AFAIK Java dates are up to millisecond precision, thus you can't format microseconds. The best you can get would be 2005-05-13 07:15:31.22200 (note that the trailing zeros would have to be appended by you, since the millisecond part would be 222 and thus would be formatted as 00222 when having the format string like SSSSS).
the standard date formats do not support microsecond precision. What you are getting is the milliseconds formatted into a 5 character wide millisecond field.
You would need to do your own manual formatting on microseconds and append it to the string yourself.
I have a JS date that is being converted by Dojo into RFC822 format. The function call -
dojo.date.toRfc3339(jsDate), generates the following date - 2007-02-26T20:15:00+02:00.
I have an application that uses a Java date SimpleDateFormat to parse in the dates generated above. I am having problems parsing this date format due to the timezone. I have attempted to use
yyyy-mm-DD'T'hh:mm:ssZ
This fails as the 'Z' for timezone doesn't expect a ':' character. Does anyone know how I would specify a pattern to handle a RFC822 date with the ':'?
revision:
Thanks for correctly interpreting what I am trying to do :) I was meant to say the date is generating in RFC3339 and I needed RFC822. Looks like I will have to override the JavaScript. I was hoping that I wouldn't have to do that and could specify a date format pattern without having to modify any Java Code as the date format is simply injected into a Spring bean of an application.
Just for completeness, is there a way to specify in a date format expression to ignore characters in the sequence (without doing String manipulation/replacement)? In this case I'd be saying ignore any ':' or just ignore the timezone all together?
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtil {
public static Date ParseRFC3339DateFormat(String p_date)
{
try{
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String dts = p_date.replaceAll("([\\+\\-]\\d\\d):(\\d\\d)","$1$2");
return formatter.parse(dts);
}catch (Exception e) {
return null;
}
}
}
This works
SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-DD'T'hh:mm:ssZ");
format.parse("2007-02-26T20:15:00+02:00".replaceAll("([\\+\\-]\\d\\d):(\\d\\d)","$1$2"));
(Note I've taking the final colon out of the format after the 'Z' format specifier.)
RFC822 does not allow a colon to be in the time zone portion of date. It expects just the 4 digits. The name of that Dojo method indicates that it is using RFC3339. It seems that is practically the same format as ISO8601. It just so happens that Joda Time has ISODateTimeFormat which is ISO8601 compatible if you are able to use that library. The method dateTimeNoMillis() looks like a match with the Dojo format. It really is nicer than the standard Java date and time API. Otherwise highlycaffeinated's suggestion would be the way to go.
Updated in response to Jamen's update
Isn't there a way to have Dojo use a format that doesn't include the timezone? Then you can adjust the format on the Java side to match. I don't know much about Dojo and I haven't been able to find any documentation on the toRfc3339 function it provides.
In Java 8 you can use:
Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse( rfc822Time ) );
FYI: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#RFC_1123_DATE_TIME
I'd strip the ':' out of the timezone and use the format you have above.
Instead of using dojo.date.toRfc3339(jsDate) you could create your own function with a custom format string.
Something like the following would remove the colon and should be parsable by your java format.
function toRfc3339String(jsDate){
return dojo.date.locale.format(jsDate,{selector: 'date', datePattern:'yyyy-MM-dd\'T\'hh:mm:ssZ'});
}
You can do this generically without Java 7. I have asked 2 questions in StackOverflow on this, within 2012, so there is a solution that does not need any third party libraries.
Just check the implementation presented in the description of my latest question, which also points to the earlier one that discusses exactly this issue.