How to display date in dd/mm/yyyy format? [duplicate] - java

This question already has answers here:
Convert java.util.Date to String
(17 answers)
How can I parse/format dates with LocalDateTime? (Java 8)
(11 answers)
Closed 8 years ago.
I am to new to Java. I want to display date in dd/mm/yyyy format. When I print date using new Date() function it gives me Sting which I don't want. Say I want to print today's date it gives me Sat Jan 24 08:17:41 IST 2015 but I want output as 24/1/2015

Use SimpleDateFormat:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String date = sdf.format(new Date());

There you have one way to do it: http://docs.oracle.com/javase/tutorial/datetime/iso/format.html
So basically you can define the format in an formatter and use that to convert
the date into a string:
try {
DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a");
String out = departure.format(format);
System.out.printf("LEAVING: %s (%s)%n", out, leavingZone);
}
catch (DateTimeException exc) {
System.out.printf("%s can't be formatted!%n", departure);
throw exc;
}

Related

Is it possible to get Date object in Android in dd-mm-yyyy format? [duplicate]

This question already has answers here:
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
return date type with format in java [duplicate]
(1 answer)
Closed 4 years ago.
In Android all Date objects are given in this type of date format "Mon Dec 03 00:13:21 GMT+05:30 2018" when you convert it with SimpleDateFormat.
Is there any possibility of getting a Date object in the "dd-mm-yyyy" format in Android?
You could set the pattern on a SimpleDateFormat and format the date:
new SimpleDateFormat("dd-mm-yyyy").format(new Date());
You could convert Strings into Dates as the following:
String pattern = "dd-mm-yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String strDate = simpleDateFormat.format(new Date());//firstly get formatted str
Date newDate = simpleDateFormat.parse(strDate);//then get date object with wanted format

Unparseable date: "2018-07-03T01:00:21.000+0000" Cannot parse this format [duplicate]

This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Java: Date parsing, why do I get an error
(3 answers)
Closed 4 years ago.
Try 1:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+/-HHmm");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = format.parse(createdDate2);
Try 2:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = format.parse(createdDate2);
Try 3:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = format.parse(createdDate2);
Nothing seems to work with this format:
Any help?
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = format.parse(createdDate2);
This might not exactly be what you want, but if the timezone offset would be written with a colon separator e.g. +00:00 it's the ISO_OFFSET_DATE_TIME
OffsetDateTime d = OffsetDateTime.parse("2018-07-03T01:00:21.000+00:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME);
System.out.println(d); // 2018-07-03T01:00:21Z

return format yyyy-mm-dd HH:mm:ss(String) to java Date [duplicate]

This question already has answers here:
Parsing ISO-8601 DateTime with offset with colon in Java
(4 answers)
Closed 6 years ago.
I have a function that needs to return a Date, I have a Date String of
'2016-02-09T12:22:00.000+00:00'
and want to convert it to
'2016-02-09 12:22:00'
but my return result is a string, in my case i want this to be a date. Here is my function so far i have tried is:
private Date parse(String s){ // function needs to return date as yyyy-mm-dd HH:mm:ss
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = null;
try {
d = sdf.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
String formattedTime = output.format(d);
System.out.println("Formated Date is = "+ formattedTime); // return date in yyyy-mm-dd HH:mm:ss (but a string)
return date; (Here i want the expected date to be as yyyy-mm-dd HH:mm:ss, I am getting as 'Tue Feb 09 12:22:00 GMT+05:30 2016')
}
Please help me its been a while trying this and am failing every time.
Thanks.
Date has no any format, it's just... date. yyyy-mm-dd HH:mm:ss is string representation according to some format (your code has this), 'Tue Feb 09 12:22:00 GMT+05:30 2016' is result of default toString().
Just parse string into Date object and return it, then call SimpleDateFormat.format(date) wherever it needs to be showed / logged / whatever.

Why SimpleDateFormat does not apply specified date pattern? [duplicate]

This question already has answers here:
Calendar date to yyyy-MM-dd format in java
(11 answers)
Closed 8 years ago.
I have following code to format a date, but the output does not match with the pattern.
try{
String date = "2014-11-1T12:14:00";
Date convertedDate = new SimpleDateFormat("yyyy-MM-dd").parse(date);
System.err.println(convertedDate.toString());
}catch(ParseException p){
System.err.println(p.getMessage());
}
Output
Sat Nov 01 00:00:00 EST 2014
I think you are questioning why you don't get the time, that's because your format doesn't include it. I suggest you use "yyyy-MM-dd'T'HH:mm:ss" to match your input and "yyyy-MM-dd" when you call DateFormat#format(Date),
try {
String date = "2014-11-1T12:14:00";
Date convertedDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(date);
System.out.println(convertedDate.toString());
// Per your comment you wanted to `format()` it like -
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(convertedDate));
} catch (ParseException p) {
System.err.println(p.getMessage());
}
For what you described below in our conversation, you can simply do this:
System.err.println((new SimpleDateFormat("yyyy-MM-dd")).format(convertedDate));

How to parse a Date+Time String [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 8 years ago.
Using java I need to parse this 2014-08-31 13:53:42.0 to 31-AUG-14 01.53.42 PM
Start by doing some research into java.text.SimpleDateFormat which can be used to parse String values of a verity of formats into a java.util.Date.
You can then use another SimpleDateFormat to format the value to the format that you want, for example
try {
String in = "2014-08-31 13:53:42.0";
SimpleDateFormat sdfIn = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
Date date = sdfIn.parse(in);
System.out.println(date);
SimpleDateFormat sdfOut = new SimpleDateFormat("dd-MMM-yy hh:mm.ss a");
System.out.println(sdfOut.format(date));
} catch (ParseException ex) {
}
Which outputs
Sun Aug 31 13:53:42 EST 2014
31-Aug-14 01:53.42 PM

Categories

Resources