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"
Related
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)
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:
SimpleDateFormat ignoring month when parsing
(4 answers)
Closed 3 years ago.
I am trying to convert date from (ddmmyyyy HH:mm:ss) to (yyyy-MM-dd HH:mm:ss.SSS) format.
Below is the code :
String startDate="06162019 00:00:00";
SimpleDateFormat inSDF = new SimpleDateFormat("ddmmyyyy HH:mm:ss");
SimpleDateFormat outSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
try{
String outDate = "";
Date date = inSDF.parse(startDate);
System.out.println(date);
outDate = outSDF.format(date);
System.out.println(outDate);
}catch (final Exception e) {
e.getMessage();
}
But i am getting wrong result :
Sun Jan 06 00:00:00 GMT 2019
2019-01-06 00:00:00.000
Any help would be appreciated?
I assume that you work with Java 8 or later. If so please drop old and horrible java.util.Date and and even worse java.text.SimpleDateFormat they are dead and buried. Switch to use of java.time package. in order to solve your problem you would need to do this:
String startDate="06162019 00:00:00";
DateTimeFormatter inSDF = DateTimeFormatter.ofPattern("MMddyyyy HH:mm:ss");
DateTimeFormatter outSDF = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
try{
System.out.println(outSDF.format(inSDF.parse(startDate)));
}catch (Exception e) {
e.getMessage();
}
See this code run live at IdeOne.com.
2019-06-16 00:00:00.000
Read about DateTimeFormatter. Also you might find this article interesting: Java 8 java.time package: parsing any string to date
A lower-case m represent minutes, while an upper-case m represents months.
You need to change SimpleDateFormat inSDF = new SimpleDateFormat("ddmmyyyy HH:mm:ss"); to SimpleDateFormat inSDF = new SimpleDateFormat("ddMMyyyy HH:mm:ss");
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:
Datetime parsing error
(2 answers)
Closed 5 years ago.
I am struggling with a date string, I need to parse into the java ‘Date’ object.
Here is what I have got so far:
try {
String value = "2017-11-23T14:00:49.184000000Z";
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'";
SimpleDateFormat parser = new SimpleDateFormat(pattern);
Date date = parser.parse(value);
} catch (ParseException e) {e.printStackTrace();}
It currently throws a ParseException “Unparseable date” and I can’t get it to work.
Any help is highly appreciated!
Thanks
Use Instant from java.time package (java 8) instead, it should look like below
String value = "2017-11-23T14:00:49.184000000Z";
Instant instant = Instant.parse(value);
Date date = Date.from(instant);
System.out.println(date);
you can use timeZone as well like this as another solution.
TimeZone tz = TimeZone.getTimeZone("Asia/Calcutta");
Calendar cal = Calendar.getInstance(tz);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'");
sdf.setCalendar(cal);
cal.setTime(sdf.parse("2017-11-23T14:58:00.184000000Z"));
Date date = cal.getTime();
System.out.println(date);