I am building an Android App, Fetching Data from API that is build in Ruby On Rails Framework.
Here is my timestamp string
2000-01-01T12:00:00.000Z
I need to convert it to Readable date and time format i.e "Saturday January 1st, 2000 12:00 am"
You can try this:
public static String convertDate(String oldDateString){
String format = "yyyy-MM-dd'T'HH:mm:ss.sssZZZZ";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = null;
try {
date = sdf.parse(oldDateString);
} catch (ParseException e) {
// handle exception here !
}
String newFormatString = "MMMM dd, yyyy 'at' HH:mm a";
SimpleDateFormat newFormatter = new SimpleDateFormat(newFormatString);
String newDateString = newFormatter.format(date);
return newDateString;
}
I had the same issue and I tried #real19 answer but it gave me errors.
I came with that then :
public static String convertDate(String oldDateString){
String format = "yyyy-MM-dd'T'HH:mm:ss.sss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = null;
try {
date = sdf.parse(oldDateString);
} catch (ParseException e) {
// handle exception here !
e.printStackTrace();
}
String newFormatString = "EEEE MMMM dd, yyyy 'at' HH:mm a";
SimpleDateFormat newFormatter = new SimpleDateFormat(newFormatString);
String newDateString = newFormatter.format(date);
return newDateString;
}
Formatting gave me jeudi décembre 22, 2016 at 16:42 PM but you can adjust your DateFormat
Related
How can I convert 24 hours time format into 12 hours format? I know this question has been asked many times, but my problem is different. My current time is:
Tue Nov 07 18:44:47 GMT+05:00 2017
I just want 6:44 pm from my date time. I tried this:
private void convertTime(String time)
{
try {
final SimpleDateFormat sdf = new SimpleDateFormat("H:mm");
final Date dateObj = sdf.parse(time);
System.out.println(dateObj);
System.out.println(new SimpleDateFormat("K:mm a").format(dateObj));
} catch (final ParseException e) {
e.printStackTrace();
}
}
final SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
Using hh will give you 12 hours format and HH 24 hour format. More details on documentation.
Edit:
Your initial format must be the following in order to parse your date string to a Date object:
final SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss 'GMT'Z yyyy");
final Date dateObj = sdf.parse(time);
After that you can format time to your needs.
From SimpleDateFormat documentation:
"h:mm a": 12:08 PM
So the format you need for:
I just want 6:44 pm from my date time
is:
final SimpleDateFormat sdf1 = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
Date date = sdf1.parse(time);
final SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
String newDateString = sdf.format(date);
try {
final SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
final Date dateObj = sdf.parse(time);
System.out.println(dateObj);
System.out.println(new SimpleDateFormat("K:mm a").format(dateObj));
} catch (final ParseException e) {
e.printStackTrace();
}
I have a routine which receives a date in the format yyyy, MMM dd, HH:mmin GMT time. I have to convert this String to a Date object and I do it with a SimpleDateFormat, but now I have to take that Date object and format it in GMT-5 using again a SimpleDateFormat, but the method is returning the same original String Date. Why? This is my routine:
public static TimeZone destinationTimeZone = TimeZone.getTimeZone("GMT-4");
public static Date parseDate(String date, String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.US);
Date d = null;
try {
d = formatter.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return d;
}
public static String formatDate(Date date, String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.US);
formatter.setTimeZone(destinationTimeZone);
return formatter.format(date);
}
#Test
public void testDateConversion() {
String strDate = "2015, Aug 03, 23:50";
Date date = DateFormatter.parseDate(strDate, "yyyy, MMM dd, HH:mm");
String dateFormatted = DateFormatter.formatDate(date, "yyyy, MMM dd, HH:mm");
assertEquals("2015, Aud 03, 19:50", dateFormatted); // Fails
}
Error message:
org.junit.ComparisonFailure:
Expected :2015, Aug 03, 19:50
Actual :2015, Aug 03, 23:50
Solved by indicating the Timezone of the receiving date string:
public static TimeZone originTimeZone = TimeZone.getTimeZone("GMT"); // +Added
public static TimeZone destinationTimeZone = TimeZone.getTimeZone("GMT-4");
public static Date parseDate(String date, String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.US);
formatter.setTimeZone(originTimeZone);// +Added
Date d = null;
try {
d = formatter.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return d;
}
public static String formatDate(Date date, String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.US);
formatter.setTimeZone(destinationTimeZone);
return formatter.format(date);
}
Try changing the String you pass in to format, like so
#Test
public void testDateConversion() {
//also changed this from 23:50 to 19:50
String strDate = "Aug 03, 2015, 19:50";
Date date = parseDate(strDate, "MMM dd, yyyy, HH:mm");
String dateFormatted = formatDate(date, "yyyy, MMM dd, HH:mm");
assertEquals("2015, Aud 03, 19:50", dateFormatted); // Fails
}
Otherwise your code is formatting as expected. The problem is, you're passing the date string in pre-formatted, so your end isn't going to change from what you started with. Your unit test is failing because you're passing in "Aug 03, 2015, 23:50" and telling Junit to expect "Aug 03, 2015, 19:50";. When you pass in a String to Date like that, the time value isn't going to change at all
I have a program in which i will get date in the below format "2015-01-17"
Please tell me how can i convert this data to this format JAN 17 2015 .
import java.text.SimpleDateFormat;
public class TestDate {
public static void main(String args[]) throws Exception
{
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
String s1 = "2015-01-17 ";
System.out.println("Result==> "+sdf1.format(sdf2.parse(s1)));
}
}
Change your sdf2 to use the MMM dd yyyy format instead...
String text = "2015-01-17";
try {
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("MMM dd yyyy");
Date date = sdf1.parse(text);
System.out.println("Result==> " + sdf2.format(date));
} catch (ParseException exp) {
exp.printStackTrace();
}
Which outputs...
Result==> Jan 17 2015
try this way
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String dateInString = "2015-01-17";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
According to the SimpleDateFormat documentation, you should be able to do that by parsing a date as follows: MMM dd yyyy.
How can i parse String "2013-03-31", so i will get - "31 March 2013 Sun".
I tried to parse as bellow:
final SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy DD");
try {
Date data = formatter.parse(eventDate);
System.out.println(data);
} catch (Exception e) {
System.out.println(e.getMessage());
}
And i get Unparseable date: "2013-03-31" (at offset 10) exception text.
Is that String represented date is correct, and if correct how i must parse it.
Input format: "yyyy-MM-dd"
Output format: "dd MMMM yyyy EEE"
final SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
final SimpleDateFormat outputFormat = new SimpleDateFormat("dd MMMM yyyy EEE");
try {
Date data = inputFormat.parse(eventDate);
System.out.println(outputFormat.format(data));
} catch (Exception e) {
System.out.println(e.getMessage());
}
see these links
string to date
How to parse a date?
How to parse date string to Date?
date to string
Convert java.util.Date to String
How to convert current date into string in java?
You don't specify a date input format:
String eventDate = "2013-03-31";
final SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
final SimpleDateFormat outputFormat = new SimpleDateFormat(
"dd MMMM yyyy EEE");
try {
Date date = inputFormat.parse(eventDate);
System.out.println(outputFormat.format(date));
} catch (Exception e) {
e.printStackTrace();
}
public static String getGmtTmFromLcl(String inputDtm, String inputFormat, String outputFormat) throws Exception {
SimpleDateFormat format = new SimpleDateFormat(inputFormat);
Date dt = format.parse(inputDtm);
format = new SimpleDateFormat(outputFormat);
return format.format(dt);
}
I'm trying to convert string to date format.I trying lot of ways to do that.But not successful. my string is "Jan 17, 2012". I want to convert this as " 2011-10-17".
Could someone please tell me the way to do this? If you have any worked through examples, that would be a real help!
try {
String strDate = "Jan 17, 2012";
//current date format
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy");
Date objDate = dateFormat.parse(strDate);
//Expected date format
SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
String finalDate = dateFormat2.format(objDate);
Log.d("Date Format:", "Final Date:"+finalDate)
} catch (Exception e) {
e.printStackTrace();
}
String format = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
Which produces this output when run in the PDT time zone:
yyyy-MM-dd 1969-12-31
yyyy-MM-dd 1970-01-01
For more info look at here
I suggest using Joda Time, it's the best and simplest library for date / dateTime manipulations in Java, and it's ThreadSafe (as opposed to the default formatting classes in Java).
You use it this way:
// Define formatters:
DateTimeFormatter inputFormat = DateTimeFormat.forPattern("MMM dd, yyyy");
DateTimeFormatter outputFormat = DateTimeFormat.forPattern("yyyy-MM-dd");
// Do your conversion:
String inputDate = "Jan 17, 2012";
DateTime date = inputFormat.parseDateTime(inputDate);
String outputDate = outputFormat.print(date);
// or:
String outputDate = date.toString(outputFormat);
// or:
String outputDate = date.toString("yyyy-MM-dd");
// Result: 2012-01-17
It also provides plenty of useful methods for operations on dates (add day, time difference, etc.). And it provides interfaces to most of the classes for easy testability and dependency injection.
Why do you want to convert string to string try to convert current time in milisecond to formated String,
this method will convert your milisconds to a data formate.
public static String getTime(long milliseconds)
{
return DateFormat.format("MMM dd, yyyy", milliseconds).toString();
}
you can also try DATE FORMATE class for better understanding.
You can't convert date from one format to other. while you are taking the date take you have take the date which ever format the you want. If you want the date in yyyy-mm-dd. You can get this by using following way.
java.util.Calendar calc = java.util.Calendar.getInstance();
int day = calc.get(java.util.Calendar.DATE);
int month = calc.get(java.util.Calendar.MONTH)+1;
int year = calc.get(java.util.Calendar.YEAR);
String currentdate = year +"/"+month +"/"+day ;
public static Date getDateFromString(String date) {
Date dt = null;
if (date != null) {
for (String sdf : supportedDateFormats) {
try {
dt = new Date(new SimpleDateFormat(sdf).parse(date).getTime());
break;
} catch (ParseException pe) {
pe.printStackTrace();
}
}
}
return dt;
}
Try this simple method:
fun getFormattedDate(strDate:String): String {
try {
val dateFormat = SimpleDateFormat("dd/mm/yyyy")//old format
val dateFormat2 = SimpleDateFormat("mm/dd/yyyy")//require new formate
val objDate = dateFormat.parse(strDate)
return dateFormat2.format(objDate)
} catch (e:Exception) {
return ""
}
}