I am getting Date Formate Response like 11:10 AM Thursday - March, 02 2017.
i have tried like this
String time = 11:10 AM Thursday - March, 02 2017
try {
Date cDate = new SimpleDateFormat("hh:mm aa EEE - MMM, dd yyyy", Locale.ENGLISH).parse(time);
System.out.println(cDate);
Calendar cal = Calendar.getInstance();
cal.setTime(cDate);
cal.setTimeZone(TimeZone.getTimeZone("UTC"));
String monthName = new SimpleDateFormat("MMMM", Locale.ENGLISH).format(cal.getTime());
String day = new SimpleDateFormat("EEEE", Locale.ENGLISH).format(cal.getTime());
int year = Calendar.getInstance().get(Calendar.YEAR);
String date = new SimpleDateFormat("dd", Locale.ENGLISH).format(cal.getTime());
String hour = new SimpleDateFormat("hh", Locale.ENGLISH).format(cal.getTime());
String min = new SimpleDateFormat("mm", Locale.ENGLISH).format(cal.getTime());
String am_pm = new SimpleDateFormat("a", Locale.ENGLISH).format(cal.getTime());
}catch (Exception e){
e.printStackTrace();
}
But it goes in exception saying:
Unparseable date: "11:10 AM Thursday - March, 02 2017" (at offset 9)
So how to pass Date Format like this String?
Well, you just missed one small thing.
you are using like :
Date cDate = new SimpleDateFormat("hh:mm aa EEE - MMM, dd yyyy", Locale.ENGLISH).parse(time);
instead, you need to use like this
Date cDate = new SimpleDateFormat("hh:mm aa EEE - MMM, dd yyyy", Locale.ENGLISH).parse(time);
You missed the one extra space of Thursday
Use EEEE for full name like Thursday
Date cDate = new SimpleDateFormat("hh:mm aa EEEE - MMM, dd yyyy", Locale.ENGLISH).parse(time);
Related
I want to calculate 4 months after the date I pulled from the database. How can I do that. The output of history is as follows.
Wed Nov 27 14:42:23 GMT+03:00 2019
JSONObject form_tarih2 = jObj.getJSONObject("form_tarih2");
String date = form_tarih2.getString("date");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
Date calculateDate = sdf.parse(date);
Try this:
JSONObject form_tarih2 = jObj.getJSONObject("form_tarih2");
String date = form_tarih2.getString("date");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
Date calculateDate = sdf.parse(date);
final Calendar calendar = Calendar.getInstance();
calendar.setTime(calculateDate);
calendar.add(Calendar.MONTH,4);
You can add/sub days, months, year etc according to your need using Calendar
Good day, I have timestamp 1481709600 and I would like to have this time format Wed, 14 Dec 2016
I'm trying to do that using:
private String getDateFromTimeStamp(Integer dt) {
Date date = new Date (dt);
return new SimpleDateFormat("EEE MMM dd hh:mm:ss yyyy ").format(date);
}
but the current output is Sun Jan 18 05:35:09 GMT+02:00 1970
I think the date format is wrong, which one I need to use ?
Thank you!
Update
the problem is that the year day and month is wrong, It should be 14 Dec 2016 instead of Jan 18 1970
Problem is your timeStamp is in seconds , so convert your timeStamp into millisec and then use the date format function ...
try this one ...
JAVA
private String getDate(long time) {
Date date = new Date(time*1000L); // *1000 is to convert seconds to milliseconds
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy "); // the format of your date
sdf.setTimeZone(TimeZone.getTimeZone("GMT-4"));
return sdf.format(date);;
}
Kotlin
fun getDate(time:Long):String {
var date:Date = Date(time*1000L); // *1000 is to convert seconds to milliseconds
var sdf:SimpleDateFormat = SimpleDateFormat("EEE, dd MMM yyyy "); // the format of your date
sdf.setTimeZone(TimeZone.getTimeZone("GMT-4"));
return sdf.format(date);
}
Output:- like this
Note:- EEE is represent as Day in Week ,MMM is represent as Month in words and so on ..
You can make any combination if you read this
You need to use EEE, d MMM yyyy
Update
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy");
sdf.format(new Date(1481709600));
the dateformat is wrong; you have to use the
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z").format(date);
try this
private String getDateFromTimeStamp(long time) {
Calendar cal = Calendar.getInstance(Locale.ENGLISH);
cal.setTimeInMillis(time);
String date = DateFormat.format("EEE MMM dd hh:mm:ss yyyy ", cal).toString();
return date;
}
Try this method to get data. Put the time in setTimeInMillis as long and not as int
private String getDate(long time) {
Calendar cal = Calendar.getInstance(Locale.ENGLISH);
cal.setTimeInMillis(time);
String date = DateFormat.format("EEE MMM dd hh:mm:ss yyyy ", cal).toString();
return date;
}
You can create a simple method to get the date from timestamps as follows
public String getDateCurrentTimeZone(long timestamp) {
try {
Calendar calendar = Calendar.getInstance();
TimeZone tz = TimeZone.getDefault(); calendar.setTimeInMillis(timestamp * 1000);
calendar.add(Calendar.MILLISECOND,
tz.getOffset(calendar.getTimeInMillis()));
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
Date currenTimeZone = (Date) calendar.getTime();
return sdf.format(currenTimeZone);
} catch (Exception e) {
}
return "";
}
This works for me:
val currentDay = Date()
val day = currentDay.time
Logger.i("currentDay = $currentDay and day : $day ")
val c = Calendar.getInstance()
c.timeInMillis = day
val d = c.time
val sdf = SimpleDateFormat("dd/MM/yyyy HH:mm")
Logger.i("meDate : ${sdf.format(d)}")
I spent about hour to solve problem, but I couldn't...
My date string is "06 Jan 2016", and I want to parse it to object Date.
I tried next method
SimpleDateFormat frmt2 = new SimpleDateFormat("dd MMM yyyy");
Date date = frmt2.parse("06 Jan 2016");
And I got:
java.text.ParseException: Unparseable date: "06 Jan 2016" (at offset 3)
I tried Joda lib
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd MMM yyyy");
DateTime dt = formatter.parseDateTime("06 Jan 2016");
But I got same error:
Invalid format: "06 Jan 2016" is malformed at "Jan 2016"
Can you help me please to obtain the success in this simple problem.
Thank you very much.
SimpleDateFormat format = new SimpleDateFormat("MMM dd,yyyy hh:mm", Locale.ENGLISH);
Date theDate = format.parse("JAN 13,2014 09:15");
Calendar myCal = new GregorianCalendar();
myCal.setTime(theDate);
System.out.println("Day: " + myCal.get(Calendar.DAY_OF_MONTH));
System.out.println("Month: " + myCal.get(Calendar.MONTH) + 1);
System.out.println("Year: " + myCal.get(Calendar.YEAR));
I'm getting call time through a cursor and want to display it in 12 hr format instead of 24 hr format.
Here is my code to get time from a cursor
String callDate = managedCursor.getString(dateIndex);
Date callDayTime = new Date(Long.valueOf(callDate));
and set this call day time to text view by
lastintreactionvalueTV.setText(callDayTime+"");
it's showing it like
Thu Jun 26 14:36:24 EDT 2014
What should I do to convert it into 12 hr format?
Use SimpleDateFormat to set the format that you need, for example:
Date callDayTime = new Date();
DateFormat sdf= new SimpleDateFormat("EEE, MMM dd KK:mm:ss a z yyyy",new Locale("en"));
System.out.println(sdf.format(callDayTime) );
This will output:
Thu, Jun 26 08:54:51 AM CEST 2014
Use this,
SimpleDateFormat read = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat write = new SimpleDateFormat("MMMM-dd-yy");
String op = write.format(read.parse("2014-05-17 15:45:56"));
//
SimpleDateFormat read = new SimpleDateFormat("input format");
SimpleDateFormat write = new SimpleDateFormat("output format");
String op = write.format(read.parse("your date as in input format"));
I want to convert the date time in
Mon APR 6, 2012 11:12 am
this format
i am using
SimpleDateFormat formatter = new SimpleDateFormat("EE MMM d, yyyy hh:mm a");
String date = formatter.format(d);
this code to get the date format but it returns
Mon Apr 6, 2012 11:12 am
i need Month in Caps and all other were same.
Is there any solution then please refer me.
Thanks
You can get Mon Apr 6, 2012 11:12 am and month as chra using chatAt() function in to string array.then convert it use toUpperCase() method in string class to make them uppr
Try this-
import java.text.SimpleDateFormat;
import java.util.Date;
class Solution
{
public static void main (String[] args)
{
Date d = new java.util.Date();
SimpleDateFormat formatter = new SimpleDateFormat("EE MMM d, yyyy hh:mm a");
String date = formatter.format(d);
String month = date.substring(4, 7);
date = date.replaceFirst(month, month.toUpperCase());
System.out.println(date);
}
}
But this doesn't work if your date format changes. You need to understand that.
Try this code
Date d = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("EE MMM d, yyyy hh:mm a");
String date = formatter.format(d);
Log.v("Test","Date==="+d);
String[] temp;
String month ;
String final_string_date = "" ;
temp = date.split(" ");
for(int i =0; i < temp.length ; i++)
{
if(i==1)
month=temp[i].toUpperCase();
else
month=temp[i];
final_string_date = final_string_date+" "+ month;
}
Log.v("Test","final_string_date==="+final_string_date.trim());
Take a look at DateFormatSymbols
http://docs.oracle.com/javase/1.4.2/docs/api/java/text/DateFormatSymbols.html
Sample :
DateFormatSymbols symbols = new DateFormatSymbols();
symbols.setShortMonths(new String[]{"JAN","FEB"....});
SimpleDateFormat format = new SimpleDateFormat("EE MMM d, yyyy hh:mm a", symbols);
public static String DateFormat(String repdate,SimpleDateFormat formater )
{
long d = Date.parse(repdate);
String[] arr= formater.format(d).split(" ");
arr[1] = arr[1].toUpperCase();
String date = "";
for(int i=0;i<arr.length;i++)
{
date = date + arr[i] +" ";
}
return date;
}
Pass your current date string and your date formater in ("EE MMM d, yyyy hh:mm a") format. you will get your solution.