How to Format Date Time in Android - java

I have an android application that returns an Entry date Formatted like this 2014-08-26T16:23:30.803
I need it to display 08/16/2014 04:23 pm
Here is my code
items.add(new ListViewItem()
{{
Ticket = json_data.optString("TicketID");
Desc = json_data.getJSONObject("Location").optString("LocationName");
Status = json_data.optString("Status_Desc");
Poster = json_data.optString("Lastposter");
Time = json_data.optString("Entrydate");
}});

public static void main(String args[]) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
SimpleDateFormat sdp = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
System.out.println(sdp.format(sdf.parse("2014-08-26T16:23:30.803")));
}
prints:
08/26/2014 04:23 PM

SimpleDateFormat sd = new SimpleDateFormat("MM'/'dd'/'yyyy hh:mm a");
sd.format(new Date());

SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");//dd/MM/yyyy
Date date= new Date("2009-09-22 16:47:08");
String strDate = sdfDate.format(date);

use this
SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy HH:mm a");
String dateInString = " 2014-08-26T16:23:30.803";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
it helps you

Related

Date Parse Exception in java java.text.ParseException

My date string is "Oct 17 2016 12:17PM"
and My date parsing method is:
public static String formatDate(String dateString) {
String stringDate = "";
try {
//Mar 11 2016 2:21PM
if (dateString != null) {
SimpleDateFormat dt = new SimpleDateFormat("MMM dd yyyy hh:mma");
Date date = dt.parse(dateString);
// *** same for the format String below
SimpleDateFormat dt1 = new SimpleDateFormat("EEE , hh:mm a");
stringDate = dt1.format(date);
}
return stringDate;
} catch (ParseException parseException) {
Log.e("date format", "" + parseException.getMessage());
}
return stringDate;
}
It is working fine with english language but when i change my app language to hindi or any other i am getting an exception.
java.text.ParseException: Unparseable date: "Oct 17 2016 12:17PM" (at offset 0)
What i do?
You need set Locale English
SimpleDateFormat dt = new SimpleDateFormat("MMM dd yyyy hh:mma", Locale.ENGLISH);
public class Practice {
public static void main(String...args) {
String day,month, finalDay;
try {
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
String currentDateTimeString = df.format(new Date());
//String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
Date date = format.parse(currentDateTimeString);
SimpleDateFormat dayFormat = new SimpleDateFormat("dd");
day = dayFormat.format(date);
SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
month = monthFormat.format(date);
System.out.print("My day"+ day);
System.out.println("My month"+ month);
} catch (Exception e) {
System.out.println("Hello exception "+ e.getMessage());
e.printStackTrace();
}
}
}

Android Convert Ruby on Rails Timestamp to Date Time

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

How to convert String into DateFormat in java?

I am working on Java. I have a String and I want to convert it into Date Format. Please see the lines of code for more details.
String dateString = "4:16:06 PM";
I want to convert it into the below Date:
Date convertedDate = 2014-04-22 16:16:06.00
How can I do this?
try this:
// get current date
SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
String a = dateFormat1.format(new Date());
// add current date to your time string
String dateString = a + " 4:16:06 PM";
SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd h:mm:ss a");
try {
// parse it. This is you date object.
Date d = dateFormat2.parse(dateString);
} catch (ParseException ex) {
ex.printStackTrace();
}
I think you are looking for following:
DateFormat df1 = new SimpleDateFormat("h:mm:ss a");
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SS");
String dateString = "4:16:06 PM";
Date date;
try {
Calendar cal = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
date = df1.parse(dateString);
cal.setTime(date);
// Reset year, month, day to current state
cal.set(Calendar.YEAR, cal2.get(Calendar.YEAR));
cal.set(Calendar.MONTH, cal2.get(Calendar.MONTH));
cal.set(Calendar.DAY_OF_MONTH, cal2.get(Calendar.DAY_OF_MONTH));
Date convertedDate = cal.getTime();
System.out.println(df2.format(convertedDate));
} catch (ParseException e) {
e.printStackTrace();
}
Try this ,
String str_date="13-09-2011";
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("dd-MM-yyyy");
date = (Date)formatter.parse(str_date);
System.out.println("Date " +date.getTime());
Try this
public class DateClass
{
public static void main(String[] args) throws Exception {
String target = "4:16:06 PM";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
DateFormat dff = new SimpleDateFormat("HH:mm:ss");
Date d=new Date();
String abc="4:16:06 PM";
Date result = new SimpleDateFormat("h:mm:ss a").parse(abc);
System.out.println("The result is "+df.format(d)+" "+dff.format(result));
}
}
Feel free to ask anything if you don't understand it and plz respond did it work or not?

How can i convert the date to aplhabetical readable format in java

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.

convert string to date and format the date

I have String variable which contains date. Now how to display July 15 from that string variable? (for example 2012-07-15)
String strdate = "2012-07-15";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MMM-dd");
try {
d1 = formatter.parse(strdate);
System.out.println(d1 + "---date first---");
} catch (ParseException e) {
e.printStackTrace();
}
Your parsing template does not match your example:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
(with MM instead of MMM).
To output July 15, you can use a second formatter:
SimpleDateFormat output = new SimpleDateFormat("MMMM dd");
System.out.println(output.format(d1)); //July 15
Something like,
try {
String strdate = "2012-07-15";
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd"");
DateFormat df2 = new SimpleDateFormat("MMMM dd");
System.out.println(df2.format(df1.parse(strdate)+ "---date first---");
}
catch (ParseException e) {
// Exception handling
}

Categories