I'm getting date from JSON Response in string format , but right now i want to that staring date into date format. How can i convert this strActiondate﹕ = 9/28/2015 6:52:41 PM into date , so that i can insert into Sq-lite database.
I have trying this way
strActiondate = jobjVessels.getString("actionDate");
Log.e("strActiondate ", " = " + strActiondate);
try{
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date netDate = (new Date(Long.parseLong(strActiondate)));
Log.e("netDate "," = "+netDate);
}
catch(Exception ex){
ex.printStackTrace();
}
I want date in same format = 9/28/2015 6:52:41 PM
This DateFormat: MM/dd/yyyy, is not valid for this date:9/28/2015 6:52:41 PM.
You need a good SimpleDateFormat that matches this string:
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
m Minute in hour Number 30
s Second in minute Number 55
So this:
String strActiondate = "9/28/2015 6:52:41 PM";
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");
Date netDate = sdf.parse(strActiondate);
System.out.println(netDate);
Will output:
Mon Sep 28 06:52:41 CEST 2015
In your code:
try{
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");
Date netDate = sdf.parse(strActiondate);
Log.d("netDate "," = "+netDate); // log.e is an error... so better d or i
}
Use this code
strActiondate = jobjVessels.getString("actionDate");
Log.e("strActiondate ", " = " + strActiondate);
try{
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");
Date netDate = sdf.parse(strActiondate);
Log.e("netDate "," = "+netDate);
}
catch(Exception ex){
ex.printStackTrace();
}
Related
I want to set a notification for this I want to get the date and time. In my database I have saved alert time and alert date differently.
alert date format is:
SimpleDateFormat df = new SimpleDateFormat("d MMM yyyy");
alert time format is:
SimpleDateFormat df = new SimpleDateFormat("hh:mm a");
Now I tried to concatenate the alert time string and alert date string and format it into date object but it is not getting format.
tried to convert into date:
public void setAlertTime() {
try {
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E MMM dd HH:mm:ss zzzz yyyy");
mAlertDateTime = mAlertDate + mAlertTime;
date = simpleDateFormat.parse(mAlertDateTime);
Log.d("AlertDate",String.valueOf(date));
Toast.makeText(AddTaskActivity.this,String.valueOf(date),Toast.LENGTH_SHORT).show();
} catch (ParseException ex) {}
}
I am not getting date value. Tried to print date value in log also in toast but it doe not show anything. What's going wrong?
You need to change two things:
add an space when concatenating date
change your SimpleDateFormat (concatenating the DateFormats with a space also):
String mAlertDate = "10 Mar 2016";
String mAlertTime = "8:00 PM";
String mAlertDateTime = mAlertDate + " " + mAlertTime;
// ↑ space!!!
// date format concatenating other date formats
SimpleDateFormat dft = new SimpleDateFormat("d MMM yyyy HH:mm a");
Date d = dft.parse(mAlertDateTime);
System.out.println(dft.format(d));
OUTPUT:
10 mar 2016 08:00 AM
for concatenate day if you merge the format then your format is
SimpleDateFormat timeStampFormat = new SimpleDateFormat("d MMM yyyy HH:mm a");
Now Add the date and time with space so it is form of format like this
mAlertDateTime = mAlertDate + " " + mAlertTime;
now Parse it to format.
date = timeStampFormat.parse(mAlertDateTime);
define your convert format
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E MMM dd HH:mm:ss zzzz yyyy");
and finally parse it.
date2 = simpleDateFormat.parse(simpleDateFormat.format(date));
Note : the Upper thing I am giving Explanation.
Now Do Just this :
Define this global :
String mAlertDate = "4 May 2016";
String mAlertTime = "01:30 PM";
Date date,date2;
and in try catch use write this code ,
try {
SimpleDateFormat timeStampFormat = new SimpleDateFormat("d MMM yyyy HH:mm a");
mAlertDateTime = mAlertDate + " " + mAlertTime;
date = timeStampFormat.parse(mAlertDateTime);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E MMM dd HH:mm:ss zzzz yyyy");
date2 = simpleDateFormat.parse(simpleDateFormat.format(date));
Toast.makeText(AddTaskActivity.this, date2.toString() ,Toast.LENGTH_SHORT).show();
} catch (ParseException ex) {}
}
Change SimpleDateFormat parameters to fit the input.
Add a space between 'mAlertDate' and 'mAlertTime'.
Don't forget to set the locale.
String mAlertDate = "10 Mar 2016";
String mAlertTime = "08:00";
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm", Locale.ENGLISH);
String mAlertDateTime = mAlertDate + " " + mAlertTime;
try {
date = simpleDateFormat.parse(mAlertDateTime);
} catch (ParseException ex) {
Logger.getLogger(LectorPDF.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(date);
Try adding space between date and time
public void setAlertTime() {
try {
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E MMM dd HH:mm:ss zzzz yyyy");
mAlertDateTime = mAlertDate + " " + mAlertTime;
date = simpleDateFormat.parse(mAlertDateTime);
Log.d("AlertDate",String.valueOf(date));
Toast.makeText(AddTaskActivity.this,String.valueOf(date),Toast.LENGTH_SHORT).show();
} catch (ParseException ex) {}
}
I have the code to change time format
String date = "13/08/13 05.57 PM";
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yy hh.mm a");
Date testDate = null;
try {
testDate = sdf.parse(date);
}catch(Exception ex){
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("dd-mm-yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..."+newFormat);
I got the output as
.....Date...13-57-2013 05:57:00 PM
instead of 13-08-2013 05:57:00 PM. Please help me to solve this issue.
Problem with mm that should be use for minutes. For months you should use MM. Read more about Date and Time Patterns in Java
Use below code
String date = "13/08/13 05.57 PM";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy hh.mm a");
Date testDate = null;
try {
testDate = sdf.parse(date);
}catch(Exception ex){
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..."+newFormat);
Use 'MM' instead of 'mm' where month is expected. Take a look at Date and Time Formats for the explanation of the different format characters.
hello I have date like..
String date="09:00 AM"
But I need 09:00:00
So I use following.
String date="09:00 AM"
SimpleDateFormat f1 = new SimpleDateFormat("hh:mm:ss");
Date d1= f1.parse(date);
But it give me parse exception.
Please help me to find this.
Thanks in advance.
That's not the correct format for parsing that date, you need something like:
"hh:mm a"
Once you have a date object, you can then use a different format to output it. The following code segment shows how:
import java.text.SimpleDateFormat;
import java.util.Date;
String date = "09:27 PM";
SimpleDateFormat h_mm_a = new SimpleDateFormat("h:mm a");
SimpleDateFormat hh_mm_ss = new SimpleDateFormat("HH:mm:ss");
try {
Date d1 = h_mm_a.parse(date);
System.out.println (hh_mm_ss.format(d1));
} catch (Exception e) {
e.printStackTrace();
}
This outputs:
21:27:00
as you would expect.
Date c = Calendar.getInstance().getTime();
//day of the month
//EEEE---full day name
//EEE- 3 chars of the day
SimpleDateFormat outFormat = new SimpleDateFormat("EEE");
String dayof_month = outFormat.format(c);
//date
//MMM--3 chars of month name
//MM--number of the month
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(c);
//time
//hh---12 hrs format
//HH---24 hrs format
// a stands for AM/PM
DateFormat date = new SimpleDateFormat("hh:mm:ss a");
String localTime = date.format(c);
login_date_text.setText(dayof_month +" , "+formattedDate);
login_time_text.setText(localTime);
Log.e("testing","date ==" +dayof_month +" , "+formattedDate);
Log.e("testing","time ==" +localTime);
Calendar ci = Calendar.getInstance();
CiDateTime = "" + (ci.get(Calendar.MONTH) + 1) +
"/" + ci.get(Calendar.DAY_OF_MONTH) +
"/" + ci.get(Calendar.YEAR);
String visitdatetime = CiDateTime + "" +
ci.get(Calendar.HOUR_OF_DAY) + ":" +
ci.get(Calendar.MINUTE) + ":" +
ci.get(Calendar.SECOND);
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
I am getting from the server in 24 hours format.but want to show my date like MM/dd/yyyy hh:mm AM/PM like 12 hrs format.
Did you try this ?
SimpleDateFormat dateFromat= new SimpleDateFormat("MM/dd/yyyy hh:mm aa");
Date today = new Date();
String todayStr = dateFromat.format(today);
Below function may be useful to you. This will convert time from 24 hours to 12 hours
public static String Convert24to12(String time)
{
String convertedTime ="";
try {
SimpleDateFormat displayFormat = new SimpleDateFormat("hh:mm a");
SimpleDateFormat parseFormat = new SimpleDateFormat("HH:mm:ss");
Date date = parseFormat.parse(time);
convertedTime=displayFormat.format(date);
System.out.println("convertedTime : "+convertedTime);
} catch (final ParseException e) {
e.printStackTrace();
}
return convertedTime;
//Output will be 10:23 PM
}
public static final String TIME_FORMAT = "hh:mm aa";
SimpleDateFormat TimeFormat = new SimpleDateFormat(TIME_FORMAT);
Calendar ATime = Calendar.getInstance();
String Timein12hourFormat = TimeFormat.format(ATime.getTime());
try with this answer this is shortest and best answer on stack.
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss aa");
Datetime = sdf.format(c.getTime());
System.out.println("============="+Datetime);
Result:-=========2015-11-20 05:52:25 PM
Here is the answer to get a data in US-English Format date/time using a 12 hour clock.
DateTime.Now.ToString("MM/d/yyyy hh:mm:ss tt");
01/16/2014 03:53:57 PM
hh:mm a
Adding the 'a' should do the trick in displaying the am/pm
In angular i am using angular-bootsrap-datetimepicker and to display the selected date and time i am using this syntax
data-ng-model="event.start | date:'dd-MM-yyyy hh:mm a'"
use below format to 12 hour with am/pm
MM/dd/yyyy HH:mm:ss a
I am getting date into string in YYYY/MM/DD HH:MM:SS format.I want to change it into the mm/dd/yyyy HH:mm:ss and also it will show AM and PM how can I do this.please help me
Thank you
To get AM PM and 12 hour date format use hh:mm:ss a as string formatter WHERE hh is for 12 hour format and a is for AM PM format.
Note: HH is for 24 hour and hh is for 12 hour date format
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
Example
String date = "2011/11/12 16:05:06";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/mm/dd HH:MM:SS");
Date testDate = null;
try {
testDate = sdf.parse(date);
}catch(Exception ex){
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..."+newFormat);
You can use the SimpleDateFormat for the same kinds of any date operations.
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
SimpleDateFormat DesiredFormat = new SimpleDateFormat("MM/dd/yyyy HH:MM:SS a");
// 'a' for AM/PM
Date date = sourceFormat.parse("2012/12/31 03:20:20");
String formattedDate = DesiredFormat.format(date.getTime());
// Now formattedDate have current date/time
Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();
just use the Time class. Try something similar to this.
Time time = new Time();
time.set(Long.valueOf(yourTimeString));
If you really need a Date object just try this.
Date date = new Date(Long.parse(yourTimeString));
Use android.text.format.Time to the conversion. You can pass the time as text and it will return you the time in desired format by using timeInstance.format("");
you need to provide formatter.
Refer to following:
Time : http://developer.android.com/reference/android/text/format/Time.html
Formatter: http://pubs.opengroup.org/onlinepubs/007908799/xsh/strftime.html
you can use any combination of formatter string to work with it :)
you can use SimpleDateFormat or DateFormat for this here is the example
SimpleDateFormat gsdf = new SimpleDateFormat("YYYY/MM/DD HH:mm:ss");
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");
try{
Date d = gsdf.parse("2011/12/13 16:17:00");
System.out.println("new date format " + sdf.format(d));
}catch(Exception){
// handle exception if parse time or another by cause
}
public static String getFormatedDate(String strDate, String sourceFormate,
String destinyFormate) {
SimpleDateFormat df;
df = new SimpleDateFormat(sourceFormate);
Date date = null;
try {
date = df.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
df = new SimpleDateFormat(destinyFormate);
return df.format(date);
}
and use like
getFormatedDate(strDate,
"yyyy-MM-dd HH:mm:ss", "mm/dd/yyyy")