long time = 1342580400; //This should be GMT: Wed, 18 Jul 2012 03:00:00 GMT
Date date = new Date(time);
SimpleDateFormat dateSdf = new SimpleDateFormat("dd MMM");
String strTime = dateSdf.format(date);
System.out.println(strTime); //This gives me 16 Jan instead.
Any ideas?
If you run the following code:
SimpleDateFormat dateSdf = new SimpleDateFormat("dd MMM yyyy hh:mm:ss Z");
Date d = dateSdf.parse("18 Jul 2012 03:00:00 GMT");
System.out.println(d.getTime());
You will get the output: 1342580400000
So your problem is that you are missing a few numbers.
Date dt = new Date();
// Fri Dec 02 17:23:13 GMT+05:30 2011
DateFormat gmtFormat = new SimpleDateFormat();
TimeZone gmtTime = TimeZone.getTimeZone("GMT");
gmtFormat.setTimeZone(gmtTime);
gmtFormat.format(dt);
Calendar new_c = gmtFormat.getCalendar();
int hours = new_c.get(Calendar.HOUR_OF_DAY);
int minutes = new_c.get(Calendar.MINUTE);
int seconds = new_c.get(Calendar.SECOND);
mYear = new_c.get(Calendar.YEAR);
MMonth = new_c.get(Calendar.MONTH);
mDay = new_c.get(Calendar.DAY_OF_MONTH);
new_c.setTimeZone(tz);
MMonth = MMonth + 1;
String curdate = mDay + "-" + MMonth + "-" + mYear;
It seems your manual calculation of date from time is wrong. I tried the following code :
long currTime = System.currentTimeMillis();
Date date = new Date(currTime);
SimpleDateFormat dateSdf = new SimpleDateFormat("dd MMM");
String strTime = dateSdf.format(date);
System.out.println(strTime);
And it gave me the correct date viz 21 Dec
Related
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);
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 am trying to get only the date from the Date object but currently i get only the following output:
"Thu Nov 24 17:46:14 GMT +02:00 2016"
and i would like to get:
"24/11/2016"
this is my code:
public String getNextYearDate(){
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, 1); // to get previous year add -1
Date nextYear = cal.getTime();
return nextYear.toString();
Just use SimpleDateFormat with dd/MM/yyyy
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
String output = df.format(nextYear);
Documentation
You can try below code :
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, 1);
int mYear = cal.get(Calendar.YEAR);
int mMonth = (cal.get(Calendar.MONTH))+1;
int mDay = cal.get(Calendar.DAY_OF_MONTH);
String result = ""+mDay+"/"+mMonth+"/"+mYear ;
I want to get time and date separately from timestamp.Please help me in these. My example of timestamp is 1378798459.
Thanks
//Try the following
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateString = formatter.format(new Date(Long.parseLong(YOUR TIMESTAMP VALUE)));
txtDate.setText(dateString);
//You can put your needed format here:
SimpleDateFormat formatter = new SimpleDateFormat("YOUR REQUIRED FORMAT");
Try this is working with me
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("yyyy-MM-dd HH:mm:ss");
Date currenTimeZone = (Date) calendar.getTime();
return sdf.format(currenTimeZone);
}catch (Exception e) {
}
return "";
}
Improving upon the answer given by Pratik Dasa
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Here you can get various formats using the following syntax. You can play around with it by deleting or adding terms given below in the syntax.
Date and Time Pattern Result
----------------------------- ---------------------------------
"yyyy.MM.dd G 'at' HH:mm:ss z" 2001.07.04 AD at 12:08:56 PDT
"EEE, MMM d, ''yy" Wed, Jul 4, '01
"h:mm a" 12:08 PM
"hh 'o''clock' a, zzzz" 12 o'clock PM, Pacific Daylight Time
"K:mm a, z" 0:08 PM, PDT
"yyyyy.MMMMM.dd GGG hh:mm aaa" 02001.July.04 AD 12:08 PM
"EEE, d MMM yyyy HH:mm:ss Z" Wed, 4 Jul 2001 12:08:56 -0700
"yyMMddHHmmssZ" 010704120856-0700
"yyyy-MM-dd'T'HH:mm:ss.SSSZ" 2001-07-04T12:08:56.235-0700
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX" 2001-07-04T12:08:56.235-07:00
"YYYY-'W'ww-u" 2001-W27-3
String time = DateUtils.formatDateTime(this, 1378798459, DateUtils.FORMAT_SHOW_TIME);
String date = DateUtils.formatDateTime(this, 1378798459, DateUtils.FORMAT_SHOW_DATE);
Try this,
final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
Date date = cal.getTime();
mHour = date.getHours();
mMinute = date.getMinutes();
Only that:
long timestampString = Long.parseLong("yourString");
String value = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").
format(new java.util.Date(timestampString * 1000));
long dv = Long.valueOf(timestamp_in_string)*1000;// its need to be in milisecond
Date df = new java.util.Date(dv);
String vv = new SimpleDateFormat("MM dd, yyyy hh:mma").format(df);
From here.
you can use this
Long tsLong = System.currentTimeMillis();
String ts = tsLong.toString();
long millisecond = Long.parseLong(ts);
datetimeString = DateFormat.format("MM-dd-yyyy hh:mm:ss a", new Date(millisecond)).toString();
timeString = datetimeString.substring(11);
dateString = datetimeString.substring(0,10);
String t2 = datetimeString.substring(20,21);
The datetimeString contains the Date Time AM/PM data
timeString will give you the substring which contains the time only and the dateString is substring for date
The String t2 will give you whether it is AM or PM in the clock
int day, month, year;
int second, minute, hour;
GregorianCalendar date = new GregorianCalendar();
day = date.get(Calendar.DAY_OF_MONTH);
month = date.get(Calendar.MONTH);
year = date.get(Calendar.YEAR);
second = date.get(Calendar.SECOND);
minute = date.get(Calendar.MINUTE);
hour = date.get(Calendar.HOUR);
String data =(hour+ ':'+ ""+minute+ ':'+"" +second+"" +""+"" +day+"" +"/" +(month+1)+"" +"/"+ +year);
Toast.makeText(getActivity(), "Time stamp:"+data,Toast.LENGTH_LONG).show();
DateFormat dateFormat = DateFormat.getDateTimeInstance();
when.setText(dateFormat.format(new Date(timestamp * 1000)));
The timestamp is multiplied by 1000 for converting the seconds into milliseconds.
All the answers are great and they mainly focus on converting the unix timestamp to milliseconds first, which is correct.
I struggled to apply that because I must use 1000L in the conversion (instead of 1000 only). Here's my working code with time zone conversion
// Set TimeZone
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yy h:mm a", Locale.US);
dateFormat.setTimeZone(getDeviceTimeZone());
// Set time
Date date = new Date(timestamp * 1000L);
return dateFormat.format(date);
For Android API 26 and above, you can just do
return Instant.ofEpochSecond( timestamp )
.atZone(ZoneId.of( timezone ))
.toLocalDateTime()
.toString();
The very best way to get day and date from the timestamp is that:
java.util.Date dayAndDate = new java.util.Date( (long) yourTimeStamp * 1000);
// object coming as like: Tue Feb 09
String day = dayAndDate.toString().split(" ")[0];
String month = dayAndDate.toString().split(" ")[1];
String date = dayAndDate.toString().split(" ")[2];
I hope you will like my approach, if you have liked it, don't forget to give it an upvote, so that others will consider it.
If you want to use time like in a WhatsApp message, You can use this method,
public static String millisToDateChat(long time) {
long currentTime = System.currentTimeMillis();
long defe = currentTime - time;
long time_in;
if(time!=0){
time_in = time;
}else{
time_in = currentTime;
defe = 0;
}
int s = (int)defe/1000;
int m = (int)defe/(1000*60);
int h = (int)defe/(1000*60*60);
int d = (int)defe/(1000*60*60*24);
int w = (int)defe/(1000*60*60*24*7);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time_in);
Date date = calendar.getTime();
#SuppressLint("SimpleDateFormat") String formattedDate=(new SimpleDateFormat("HH:mm")).format(date);
#SuppressLint("SimpleDateFormat") String formattedYear=(new SimpleDateFormat("MMM d, ''yy")).format(date);
#SuppressLint("SimpleDateFormat") String formattedm=(new SimpleDateFormat("MMM d")).format(date);
if(d>365) {
return formattedYear;
}else if(s>172000){
return formattedm;
}else if(s>86400) {
return "Yest.";
}else{
return formattedDate;
}
}