This question already has an answer here:
How to handle upper or lower case in JSR 310? [duplicate]
(1 answer)
Closed 7 years ago.
I'm trying to parse date time string to LocalDateTime. However if I send month with all caps its thorwning an error, is there any workaround. Here is the below code
#Test
public void testDateFormat(){
DateTimeFormatter formatter= DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse("04-NOV-2015 16:00:00", formatter); //if I send month Nov it works
System.out.println(dateTime.getYear());
}
The Same works for simpleDateFormat
#Test
public void testSimpleDateTime() throws ParseException{
SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
Date dateTime = format.parse("04-NOV-2015 16:00:00");
System.out.println(dateTime.getTime());
}
Answering this question because most of us might not know JSR 310. Hence would search for java 8 LocalDateTime ignore case.
#Test
public void testDateFormat(){
DateTimeFormatter formatter= new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("dd-MMM-yyyy HH:mm:ss").toFormatter();
LocalDateTime dateTime = LocalDateTime.parse("04-NOV-2015 16:00:00", formatter);
System.out.println(dateTime.getYear());
}
**UPDATE*
To locale
DateTimeFormatter parser = new DateTimeFormatterBuilder().parseCaseInsensitive() .appendPattern("dd-MMM-yyyy HH:mm:ss.SS").toFormatter(Locale.ENGLISH)
Related
This question already has answers here:
Java SimpleDateFormat Timezone offset with minute separated by colon
(2 answers)
Closed 2 years ago.
public class DatePgm {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'+'SS:SZ");
Date date1 = sdf.parse("2020-07-26T18:52:24+05:30");
System.out.println("DATE="+sdf.format(date1));
}
}
Can any one help me to print the exact date and time format "2020-07-26T18:52:24+05:30"
tl;dr ⇒ java.time.OffsetDateTime
In this case, you don't really need to define a format/formatter yourself. The default DateTimeFormatter of an OffsetDateTime (which appears suitable here) is able to parse (and print/format) your example String. You can do it as follows:
public static void main(String[] args) {
// your example datetime String
String datetime = "2020-07-26T18:52:24+05:30";
// parse the String to an OffsetDateTime using the default formatter
OffsetDateTime odt = OffsetDateTime.parse(datetime);
// and print the OffsetDateTime using its default formatter
System.out.println("DATE=" + odt);
}
the output is
DATE=2020-07-26T18:52:24+05:30
Use Instant instead of Date. Instant supports date time with zone
Instant date1 = Instant.parse("2020-07-26T18:52:24+05:30");
System.out.println("DATE= " + date1);
You can try following,
String string = "2020-07-26T18:52:24+05:30";
OffsetDateTime odt = OffsetDateTime.parse( string );
System.out.println(odt);
You can use Calendar to get the date:
String time = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'+'SS:SZ").format(Calendar.getInstance().getTime());
I think you should use LocalDatetIme, from java 8. A very good example for most of the formats are available here
With SimpleDateFormat you have to set the timzone(Asia/Colombo is +05:30):
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
Date date1 = sdf.parse("2020-07-26T18:52:24+05:30");
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Colombo"));
System.out.println("DATE="+sdf.format(date1));
Output:
DATE=2020-07-26T18:52:24+05:30
This question already has answers here:
Android: Compare time in this format `yyyy-mm-dd hh:mm:ss` to the current moment
(5 answers)
Conversion of a date to epoch Java [duplicate]
(4 answers)
How to get the current time in YYYY-MM-DD HH:MI:Sec.Millisecond format in Java?
(16 answers)
Closed 2 years ago.
The following code gave me Datetimestamp as [ 2020-07-183 17:07:55.551 ]. The issue is with "Day" in Datetimestamp, which has three digits. How to format currentTimeMillis into the right format for day of month?
public String Datetimesetter(long currentTimeMillis, SimpleDateFormat dateFormat) {
dateFormat = new SimpleDateFormat("YYYY-MM-DD HH:MM:SS.SSS");
// Create a calendar object that will convert the date and time value in milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(currentTimeMillis);
return dateFormat.format(calendar.getTime());
}
SOLUTION WHICH WORKED FOR ME:
Please visit this link.
This is for the case you are supporting Apps from API level 26 (native support of java.time) or you are willing / allowed to use a backport library of the same functionality.
Then you can use a correct / matching pattern (one that considers three-digit days) like this:
public static void main(String[] args) {
// mock / receive the datetime string
String timestamp = "2020-07-183 17:07:55.551";
// create a formatter using a suitable pattern (NOTE the 3 Ds)
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-DDD HH:mm:ss.SSS");
// parse the String to a LocalDateTime using the formatter defined before
LocalDateTime ldt = LocalDateTime.parse(timestamp, dtf);
// and print its default String representation
System.out.println(ldt);
}
which outputs
2020-07-01T17:07:55.551
So I guess the day of year no. 183 was actually July 1st.
your date format is incorrect
dateFormat = new SimpleDateFormat("YYYY-MM-DD HH:MM:SS.SSS");
change to this
dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS.SSS");
This question already has answers here:
display Java.util.Date in a specific format
(11 answers)
Java string to date conversion
(17 answers)
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
Closed 3 years ago.
I have string with format : '20018-03-03 11:00:00', and i want to convert to Date but keeping this format. Is this possible? Because when I do something like this :
Date.parse(string), I don't get this format, event when I use
SimpleDateFormat. What I'm doing wrong ?
I tried this:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
LocalDateTime now = LocalDateTime.parse(entry.getValue(), formatter);
String formatDateTime = now.format(formatter);
// Date date = df.parse(sqlDate);
Date d = (Date) formatter.parse(formatDateTime);
It might not be the case in other languages, but in Java the format used to parse a date is not stored in the date itself. Thus you have to reuse the same format when you want to print (format) a date.
Old (Date, SimpleDateFormat) and new (LocalDateTime, etc.) API should not be mixed together. Stick to the new one unless you have legacy code to deal with.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// Parse: String -> LocalDateTime
LocalDateTime now = LocalDateTime.parse("2018-03-03 11:00:00", formatter);
// Format: LocalDateTime -> String
System.out.println(now.format(formatter));
This question already has answers here:
Is java.time failing to parse fraction-of-second?
(3 answers)
Closed 5 years ago.
I'm trying to convert a string into a LocalDateTime object.
#Test
public void testDateFormat() {
String date = "20171205014657111";
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
LocalDateTime dt = LocalDateTime.parse(date, formatter);
}
I would expect this test to pass.
I get the following error:
java.time.format.DateTimeParseException: Text '20171205014657111' could not be parsed at index 0
Looks like I may have run across this bug: https://bugs.openjdk.java.net/browse/JDK-8031085 as it corresponds to the JVM version I'm using. The workaround in the comments fixes the issue for me:
#Test
public void testDateFormat() {
String date = "20171205014657111";
DateTimeFormatter dtf = new DateTimeFormatterBuilder()
.appendPattern("yyyyMMddHHmmss")
.appendValue(ChronoField.MILLI_OF_SECOND, 3).toFormatter();
LocalDateTime dt = LocalDateTime.parse(date, dtf);
}
This question already has answers here:
Joda-Time Formatter with a dateStyle and a timeStyle
(2 answers)
Closed 8 years ago.
I want format the current date to a format, I can do that in the following lines
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");
Date currentDate = new Date();
String currentDateString = dateFormat.format(currentDate);
try {
currentDate = dateFormat.parse(currentDateString);
} catch (ParseException e) {
}
But is there a way more clear and tidy to achieve that ?
Use Java 8 and the new classes LocalDateTime and DateTimeFormatter to format to a String. Don't use Date from Java 7 and lower. That old API is a mess.
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd:HH:mm:ss")));
Output:
2014-06-08:14:43:01
You don't have to migrate to Java 8. If use java 6 or 7, I can use Joda-Time library.
JSR-310 form Java 8 is started from scratch, but with an API 'inspired by Joda-Time'.
Here you have example:
LocalDate date = LocalDate.now();
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd:HH:mm:ss");
String str = date.toString(fmt);
// might output "6 October, 2013"