Converting Date to String in SimpleDateFormat [duplicate] - java

This question already has answers here:
How to convert current date into string in java?
(9 answers)
Closed 6 years ago.
I want to convert the date format to string, but the string output is not coming
DateFormat currentDate = new SimpleDateFormat("dd MM yyyy ");
String currDateString = String.valueOf(currentDate);
System.out.println(currDateString);

To format current date you need to create a instance of Date which represents current date and then format it using SimpleDateFormat to string.
DateFormat dateFormat = new SimpleDateFormat("dd MM yyyy");
String currDateString = dateFormat.format(new Date());
System.out.println(currDateString);

Related

How I can get date object in format yyyy-MM-dd after date parsing [duplicate]

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);

How to convert into dd-mmm-yyyy format to dd-mm-yyyy format in swing(core java)? [duplicate]

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

Date conversion in MM/DD/YY h:mm:ss a format in java [duplicate]

This question already has answers here:
"EEE MMM dd HH:mm:ss ZZZ yyyy" date format to java.sql.Date
(2 answers)
Java string to date conversion
(17 answers)
Calendar.getInstance gives wrong date [duplicate]
(2 answers)
Closed 4 years ago.
I am trying to get this date 09/03/18 6:30:00 PM using 4-SEP-2018 but I am getting 12/364/17 6:30:00 PM, Here is what I have tried.
public class StockBuySell
{
static String DATE_FORMAT_UI = "DD-MMM-YYYY";
public static void main(String args[]) throws Exception {
DateFormat outputFormat = new SimpleDateFormat("MM/DD/YY h:mm:ss a");
outputFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
DateFormat inputFormat = new SimpleDateFormat(DATE_FORMAT_UI);
String inputText = "4-SEP-2018";
Date date = inputFormat.parse(inputText);
String outputText = outputFormat.format(date);
System.out.println(outputText);
}
}
As per the JavaDoc, D stands for Day in year. You will need to replace DD with dd (day in month).
Y stands for Week year, not year.
In short, this: "DD-MMM-YYYY" needs to be this: "dd-MMM-yyyy".
Try this:
String DATE_FORMAT_UI = "dd-MMM-yyyy";
DateFormat outputFormat = new SimpleDateFormat("MM/dd/yy h:mm:ss a");
outputFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
DateFormat inputFormat = new SimpleDateFormat(DATE_FORMAT_UI);
String inputText = "4-SEP-2018";
Date date = inputFormat.parse(inputText);
String outputText = outputFormat.format(date);
System.out.println(outputText);
You are going to have time zone issues as you're forcing the GMT... the date you're rendering will be in the local time zone so it may change the date.

Java how to convert UK into java.util.Date [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 7 years ago.
I'm retrieving a date in a HTML form, and I need to convert the string date into a java.util.Date so that it can be inserted into a database.
I've tried the following, but it always gives me a date in May 2008.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date myDate = formatter.parse("27-11-2015");
String string = "2015-11-27";
DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date);
This was user error, my code should have been:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Date myDate = formatter.parse("27-11-2015");

Date format in java [duplicate]

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);

Categories