Convert date to string in java? [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In my code I am getting date as this String:
date="2013-06-15"
I want it to convert to 15 Jun 2013 . I have tried dateFormat:
DateFormat df = new SimpleDateFormat("yyyy-mm-dd");
String date = df.format(det);
DateFormat f2 = new SimpleDateFormat("dd-mmmm-yyyy");
date=f2.format(date).toLowerCase();
out.println("DATE"+date);
But it gave null pointer Could Anyone help me doing this.Please help?

String dateString="2013-06-15";
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(dateString);
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
DateFormat df2 = new SimpleDateFormat("dd MMM yyyy");
System.out.println(df.format(date).toLowerCase());//print 15-jun-2013
System.out.println(df.format(date));//print 15-Jun-2013
System.out.println(df2.format(date));//print 15 Jun 2013

first convert your Date string to Date then convert it to your required format
package naveed.workingfiles;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class DateToString {
public static void main(String[] args) {
String laDate="2013-06-15";
String dateString = laDate.substring(8, 10) + "/"
+ laDate.substring(5, 7) + "/"
+ laDate.substring(0, 4);
Date date= new SimpleDateFormat("dd/MM/yyyy").parse(dateString);
String dateFormat = "dd-MMM-yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, new Locale("en_US"));
String tDate = sdf.format(date);
System.out.println(tDate);//here your String date
}
}

// Create an instance of SimpleDateFormat used for formatting
// the string representation of date (month/day/year)
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
// Get the date today using Calendar object.
Date today = Calendar.getInstance().getTime();
// Using DateFormat format method we can create a string
// representation of a date with the defined format.
String reportDate = df.format(today);
// Print what date is today!
System.out.println("Report Date: " + reportDate);

This will work
String dateStr = "2013-06-15";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date=df.parse(dateStr);
DateFormat f2 = new SimpleDateFormat("dd-MMM-yyyy");
System.out.println(f2.format(date));

This is not gonna work: format() does not take String parameters:
String date = df.format(det);
DateFormat f2 = new SimpleDateFormat("dd-mmmm-yyyy");
date=f2.format(date).toLowerCase();
Try this:
String dateAsString = "2013-06-15"
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = df.parse(dateAsString);
df = new SimpleDateFormat("dd MMM yyyy");
out.println("DATE"+df.format(date));

Related

How change String to Date format

I have this string: 2018-09-22 10:17:24.772000
I want to convert it to Date:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
String sdate = "2018-09-22 10:17:24.772000";
Date dateFrom = simpleDateFormat.parse(sdate);
but it shows: Sat Sep 22 10:17:24 GMT+03:30 2018
Here is what you should do instead, you are printing date object itself, you should print its format.
I will provide the code with old date api and new local date api :
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
String sdate = "2018-09-22 10:17:24.772000";
Date dateFrom = simpleDateFormat.parse(sdate);
System.out.println(dateFrom); // this is what you do
System.out.println(simpleDateFormat.format(dateFrom)); // this is what you should do
// below is from new java.time package
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
System.out.println(LocalDateTime.parse(sdate, formatter).format(formatter));
output is :
Sat Sep 22 10:30:16 EET 2018
2018-09-22 10:30:16.000000
2018-09-22 10:17:24.772000
Hope This will help you
public class Utils {
public static void main(String[] args) {
String mytime="2018-09-22 10:17:24.772000";
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss.SSSSSS");
Date myDate = null;
try {
myDate = dateFormat.parse(mytime);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd");
String finalDate = timeFormat.format(myDate);
System.out.println(finalDate);
}
}
Looks to me like you have converted it to a Date. What is your desired result? I suspect what you are wanting to do is to create another Simple date format that shows your expected format and then use simpledateformat2.format(dateFrom)
I should also point out based on past experience that you should add a Locale to your simple date formats otherwise a device with a different language setting may not be able to execute this code
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS", Locale.US);

how to format date using SimpleDateFormat

I cannot format a date.
dateFormat.format() accepts a Date as argument. So I created a new Date()
It says the below Date() method is deprecated, and I get the below exception while running.
exception:
Exception in thread "main" java.lang.IllegalArgumentException at
java.util.Date.parse(Date.java:598)
public class MyDate {
public static void main(String[] args) {
Date date = new Date("2012-02-16T00:00:00.000-0500");
SimpleDateFormat dateFormat = new SimpleDateFormat(
"dd-MMM-yyyy HH:mm:ss");
String stringDate = dateFormat.format(date);
System.out.println(stringDate); // how do I test this conversion??
}
}
My database has date of the format - 2012-02-16T00:00:00.000-0500
I need to convert it to string of the format : dd-MMM-yyyy HH:mm:ss
I'm using Java6
Thanks to #Andy Brown. In addition to what Andy Brown has answered, I'm posting the complete snippet
Complete Solution:
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SampleDate {
public static void main(String[] args) throws ParseException {
DateFormat parseFormat = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = parseFormat.parse("2012-03-16T00:00:00.000-0500");
String strDate = parseFormat.format(date);
System.out.println(strDate);
// if you get date of type 'java.sql.Date' directly from database cursor like
//rs.getDate("created_date"), just pass it directly to format()
SimpleDateFormat dateFormat = new SimpleDateFormat(
"dd-MMM-yyyy HH:mm:ss");
String stringDate = dateFormat.format(date);
System.out.println(stringDate);
}
}
/*
Output:
2012-03-16T01:00:00.000-0400
16-Mar-2012 01:00:00
*/
you can also convert java.util.Date to java.sql.Date like this,
String dateString = "03-11-2012";
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
java.util.Date date = dateFormat.parse(dateString);
java.sql.Date sqlDate = new Date(date.getTime());
// set the input param type as OracleTypes.DATE and pass the input param date as sqlDate
If you want to read in the date "2012-02-16T00:00:00.000-0500" you should probably use a SimpleDateFormat to parse it like so:
DateFormat parseFormat = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = parseFormat.parse("2012-02-16T00:00:00.000-0500");
Along with the rest of your code this writes:
16-Feb-2012 05:00:00
The parse format pattern letters are listed in the SimpleDateFormat documentation. The T is escaped with apostrophes.
This answer assumes Java 7, or you would be using the new date & time API from Java 8

How to convert date in to String MM/dd/yyyy Format? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a Date 2014-10-01 00:00:00.0
I have to convert into 10/01.2014
How can i?
how about this one
try
{
String currentDate = "2014-10-01 00:00:00.0";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
Date tempDate=simpleDateFormat.parse(currentDate);
SimpleDateFormat outputDateFormat = new SimpleDateFormat("dd/MM.YYYY");
System.out.println("Output date is = "+outputDateFormat.format(tempDate));
} catch (ParseException ex)
{
System.out.println("Parse Exception");
}
Use SimpleDateFormat:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String date = sdf.format("Your date");
try This
// Create an instance of SimpleDateFormat used for formatting
// the string representation of date (month/day/year)
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
// Get the date today using Calendar object.
Date today = Calendar.getInstance().getTime();
// Using DateFormat format method we can create a string
// representation of a date with the defined format.
String reportDate = df.format(today);
// Print what date is today!
System.out.println("Report Date: " + reportDate);
It's pretty easy if you use the JodaTime library.
DateTime dt = new DateTime();
DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/dd.YYYY");
String str = fmt.print(dt);
try this:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
Date d = sdf.parse("dd/MM.YYYY");

Format current date to show day of the week [duplicate]

This question already has answers here:
How to determine day of week by passing specific date?
(28 answers)
Closed 6 years ago.
I want to show the current date in my application like this:
Thu, May 2, 2013
I already have the following code to get the current date
Calendar c = Calendar.getInstance();
Time time = new Time();
time.set(c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.MONTH),
c.get(Calendar.YEAR));
How can I format this Time object to the string I need?
This does what you want
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, yyyy");
String strDate = sdf.format(cal.getTime());
System.out.println("Current date in String Format: " + strDate);
Where strDate can be displayed in your textView or whatever
Maybe you can use it.
This example displays the names of the weekdays in short form with the help of DateFormatSymbols().getWeekdays() method of DateFormatSymbols class.
import java.text.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Date dt = new Date(1000000000000L);
DateFormat[] dtformat = new DateFormat[6];
dtformat[0] = DateFormat.getInstance();
dtformat[1] = DateFormat.getDateInstance();
dtformat[2] = DateFormat.getDateInstance(DateFormat.MEDIUM);
dtformat[3] = DateFormat.getDateInstance(DateFormat.FULL);
dtformat[4] = DateFormat.getDateInstance(DateFormat.LONG);
dtformat[5] = DateFormat.getDateInstance(DateFormat.SHORT);
for(DateFormat dateform : dtformat)
System.out.println(dateform.format(dt));
}
}
output:
9/9/01 7:16 AM
Sep 9, 2001
Sep 9, 2001
Sunday, September 9, 2001
September 9, 2001
9/9/01
Source
Use this
SimpleDateFormat formatter = new SimpleDateFormat("EEE, MMM dd,yyyy");
String formattedDate = formatter.format(new Date(System.currentTimeMillis()));
Log.e("formattedDate",formattedDate);
I will suggest to use java.text.SimpleDateFormat instead.
public static void main(String[] args) {
Date date=new Date();
String format = new SimpleDateFormat("EEE,MMM d,yyyy ").format(date);
System.out.println(format);
}
SimpleDateFormat dateformat= new SimpleDateFormat("dd,MM,yyyy");
String strdate = dateformat.format(new Date(System.currentTimeMillis()));
Oops, I'm a bit slow.

String Date Value Converting

I am converting String to Date format. But it returns "Unparseable date". for example,
String date= "Wednesday, May 15, 2013";
I want to convert this to String like "2013-05-15" How to do that?
Use SimpleDateFormat twice: Once to parse a Date, the other to render it in the desired format:
Date date;
String display = new SimpleDateFormat("yyyy-MM-dd").format(
new SimpleDateFormat("EEEE, MMMM dd, yyyy").parse(date)
);
Your example date is unfortunate, because it uses the only 3-letter month "May", so I can't tell if your month names are all truncated to 3 letters, or if they are the full name. I have assumed months to be the full name, but if they are truncated, change MMMM to MMM in the second format string.
Something like this might help (parse the date string to date object and format it back in the new format):
String dateString = "Wednesday, May 15, 2013";
DateFormat format1 = new SimpleDateFormat("EEEE, MMMM dd, yyyy");
Date date = format1.parse(dateString);
DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
String updatedDateString = format2.format(date);
System.out.println("Updated Date > "+updatedDateString);
In my experiments with this, you need to do something like the below...Refer to the API for understanding how to construct your format strings. http://docs.oracle.com/javase/6/docs/api/index.html?java/text/DateFormat.html
String myDateAsString = "Wednesday, May 15, 2013";
SimpleDateFormat df = new SimpleDateFormat("EEEE, MMM d, yyyy");
Date d = new Date();
try {
d = df.parse(myDateAsString);
} catch (ParseException e1) {
System.out.println("Could not parse...something wrong....");
e1.printStackTrace();
}
df.applyPattern("yyyy-MM-d");
String convertedDate = df.format(d);
System.out.println(convertedDate);
This will be a good approach.
Something like this:
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringDate {
public static void main(String[] args) throws ParseException{
String dateString = "Wednesday, May 15, 2013";
DateFormat format1 = new SimpleDateFormat("E, MMM dd, yyyy");
Date date = format1.parse(dateString);
DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
String updatedDateString = format2.format(date);
System.out.println("Updated Date > "+updatedDateString);
}
}

Categories