This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
how to convert seconds_since_the_beginning_of_this_epoch to date format in java..?
hi i have "1304054138" in ssboetod . I want to display it in this "21 Apr 2011 11:46:00 AM IST" format. How i can do this in java code... ?
SimpleDateFormat sdf = new SimpleDateFormat("dd MM yyyy hh:mm:ss a z")
String result = sdf.format(new Date(timestamp));
if timestamp is a String, you can obtain the long version by calling Long.parseLong(string)
Like this:
// creat date format
DateFormat dateFormat = DateFormat.getDateTimeInstance(
DateFormat.LONG, DateFormat.LONG);
// set time zone
dateFormat.setTimeZone(TimeZone.getTimeZone("IST"));
// create and format date
String formattedDate = dateFormat.format(new Date(Long.valueOf("1304054138")));
// write out
System.out.println(formattedDate);
Related
This question already has answers here:
Java convert date format [duplicate]
(3 answers)
display Java.util.Date in a specific format
(11 answers)
Closed 2 years ago.
I have string date 2020-07-05 22:44:59 in string format, I parse it with SimpleDateFormat with
"yyyy-MM-dd HH:mm:ss a z" date pattern but it gives me date like Sun Jul 05 22:44:59 IST 2020.
So how I can get date in "yyyy-MM-dd HH:mm:ss a z" format in date object.
Input : 2020-07-05 22:44:59
Output : 2020-07-05 22:44:59
I want output int the same format like input.
Thanks for Helping me.
String input = "2020-07-05 22:44:59";
DateFormat parser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = parser.parse(input);
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String output = formatter.format(date);
This question already has answers here:
Change date format in a Java string
(22 answers)
How can I change the date format in Java? [duplicate]
(10 answers)
ParseException when parsing 3 character abbreviated month using SimpleDateFormat
(5 answers)
Convert String Date to String date different format [duplicate]
(8 answers)
Closed 3 years ago.
To convert dd-mmm-yyyy to dd-mm-yyyy format.
String dateofbirth = ((JTextField) dobcalender.getDateEditor().getUiComponent()).getText();//date from jcalender
SimpleDateFormat myFormat = new SimpleDateFormat("dd-MM-yyyy");
tried these codes :
System.out.println(myFormat.format(myFormat.parse(dateofbirth)));
myFormat.format(myFormat.parse(dateofbirth));
showing error parseexception
Based on the format "24 Feb 2019"
SimpleDateFormat from=new SimpleDateFormat("dd MMM yyyy");
SimpleDateFormat to=new SimpleDateFormat("dd-MM-yyyy");
Date frm=from.parse("24 Feb 2019");
System.out.println(frm);//Sun Feb 24 00:00:00 IST 2019
System.out.println(to.format(frm));//24-02-2019
First, you need to parse the String to Date object. Then you need to convert the Date object to a new formatted String. Here is the sample code:
String dateofbirth = "09-10-2010"; //date from jcalender
SimpleDateFormat myFormat = new SimpleDateFormat("dd-MM-yyyy");
// converting to Date object
Date date = myFormat.parse(dateofbirth);
SimpleDateFormat myFormat1 = new SimpleDateFormat("dd-MMM-yyyy");
// converting Date object to new format
String formattedDate = myFormat1.format(date);
System.out.println(formattedDate); // prints: 09-Oct-2010
// Before Java8 , No thread safe , has to import multiple packages(util, text), throws checked Exception(ParseException)
System.out.println(oldDate); //21 Jul 2019
SimpleDateFormat oldPattern = new SimpleDateFormat("dd MMM yyyy");
SimpleDateFormat newPattern = new SimpleDateFormat("dd-MM-yyyy");
try {
Date date = oldPattern.parse(oldDate);
String newDate = newPattern.format(date);
System.out.println(newDate); //21-07-2019
} catch (ParseException e) {
// Exception handling message/mechanism/logging as per company standard
}
// In Java8, Thread Safe, Immutable, throws unchecked Exception(DateTimeParseException)
DateTimeFormatter oldPattern8 = DateTimeFormatter.ofPattern("dd MMM-yyyy");
DateTimeFormatter newPattern8 = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate datetime = LocalDate.parse(oldDate, oldPattern8);
String output = datetime.format(newPattern8);
System.out.println(output); //21-07-2019
This question already has answers here:
Setting date into "dd"-"mm"-"yy" format from "dd"-"mm"-"yyyy" in java
(4 answers)
Closed 6 years ago.
I have a date in the format 11-NOV-13
When i convert the date i get the value Sat Nov 11 00:00:00 IST 13.
But i want the converted date to be in the format Sat Nov 11 00:00:00 IST 2013.
Though i have used the dateformat it is not taking it. Can anyone point me where i am wrong?
Below is the code i have been working on
DateFormat format = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
Date myDate = format.parse("11-NOV-13");
Change the format to:
DateFormat format = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);
You need to make the year 2 numbers:
DateFormat format = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);
You can reference the docs here.
As you are not providing the full year you need
DateFormat format = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);
Date myDate = format.parse("11-NOV-13");
System.out.println(myDate);
Change to this format:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
DateFormat format = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);
Date myDate = format.parse("11-NOV-13");
String formattedDate = sdf.format(myDate);
This question already has answers here:
Java date conversion [closed]
(2 answers)
Closed 9 years ago.
date from database
2013-10-26T10:31:20GMT+05:30
UI Date
Mon Feb 10 00:00:00 GMT+05:30 2014
need to convert according to Database Date
Please try this
Date dNow = new Date( );
SimpleDateFormat ft =
new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
ft.format(dNow);
Note: give your format to SimpleDateFormat ,it will then format as shown above.
Use following code
// This is how to get today's date in Java
Date today = new Date();// use your date here
//SimpleDateFormat example - Date with timezone information
SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm 'GMT'Z '('z')'");
String date = DATE_FORMAT.format(today);
System.out.println("Today in yyyy-MM-dd'T'HH:mm 'GMT'Z '('z')' : " + date);
This question already has answers here:
Convert string to date then format the date
(8 answers)
Closed 9 years ago.
Given DateTime, how to format date in the following format ?
12 JUL 2013
in c#, you do this
string formatedDateString = String.Format("{0:dd MMM yyyy}", myDateTime);
What is the equivalent code in JAVA ?
And date represents instance of java.util.Date
String formattedDate = new SimpleDateFormat("dd MMM yyyy").format(date);
Use a SimpleDateFormat. Along the lines of
(new SimpleDateFormat("dd MMM yyyy")).parse("12 JUL 2013");
(new SimpleDateFormat("dd MMM yyyy")).format(new Date());
I would use the localized approach as described here:
String formattedDate = DateFormat.getDateInstance().format(date);