From timestamp to Date Android - android

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)}")

Related

Convert MM/dd/yyyy hh:mm a format to milliseconds

I am trying to Convert date and time i am receiving in MM/dd/yyyy hh:mm a format to milliseconds so that i can push the date as end date in google calendar. I am trying below code but i am getting error.
Code:
String myDate = "10/20/2017 8:10 AM";
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
Date date = sdf.parse(myDate);
long millis = date.getTime();
Error:
java.text.ParseException: Unparseable date: "10/20/2017 8:10 AM" (at offset 16)
Try this .
String myDate = "10/20/2017 8:10 AM";
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy h:mm a", Locale.ENGLISH);
Date date = sdf.parse(myDate);
long millis = date.getTime();
Add Locale.ENGLISH in your code .
Your code seems to be OK, i've just added try/catch block:
try {
String myDate = "10/20/2017 8:10 AM";
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
Date date = sdf.parse(myDate);
long millis = date.getTime();
System.out.println(date);
System.out.println(millis);
}
catch(Exception ex) {
System.out.println(ex);
}
This code prints 1508479800000 as millis. If you want to check it backwards try this:
String x = "1508479800000";
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
long milliSeconds= Long.parseLong(x);
System.out.println(milliSeconds);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
System.out.println(formatter.format(calendar.getTime()));
this will give you 10/20/2017 08:10 AM.
You can use SimpleDateFormat to do it. You just have to know 2 things.
All dates are internally represented in UTC
.getTime() returns the number of milliseconds since 1970-01-01 00:00:00 UTC.
package se.wederbrand.milliseconds;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String inputString = "00:01:30.500";
Date date = sdf.parse("1970-01-01 " + inputString);
System.out.println("in milliseconds: " + date.getTime());
}
}
by this one https://stackoverflow.com/a/8826392/8456432

Android Convert DateTime to EEE, d MMM yyyy

I am trying to convert the following data time to EEE, d MMM yyyy format but kept getting Unparsable date format. Can some one please help. I couldn't find anything on the web that helped.
Here is the code
String datestr = "2017-01-12T00:00:00Z";
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
Date convertedDateStart = new Date();
try {
convertedDateStart = dateFormat.parse(datestr);
camp_new.startdate = convertedDateStart;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Any help wound be greatly appreciated. Thanks in advance
The date string "2017-01-12T00:00:00Z" cannot be parsed with your format "EEE, d MMM yyyy HH:mm:ss Z"; the fields are the wrong type or in the wrong place.
The format for your datestr "2017-01-12T00:00:00Z" should be:
yyyy-MM-dd'T'HH:mm:ssZ
You need to initialize the SimpleDateFormat with this format to parse datestr.
The format "EEE, d MMM yyyy HH:mm:ss Z" would parse a date that looks like
Thu, 1 Jan 2017 00:00:00 Z
Read the docs to understand the difference between parse() and format().
You have to parse it first, store it as a Date object and then use another SimpleDateFormat to format in the desired way.
String datestr = "2017-01-12T00:00:00Z";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US);
SimpleDateFormat dateFormat2 = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
Date date = dateFormat.parse(datestr);
String convertedDateStart = dateFormat2.format(date);
Well This will Work For you if you want to FORMAT and get a Date String with that format..
String datestr = "2017-01-12T00:00:00Z";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
try {
date = dateFormat.parse(datestr);
} catch (ParseException e) {
e.printStackTrace();
}
dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
datestr = dateFormat.format(date);
System.out.println(datestr);
This will give you Thu, 12 Jan 2017 00:00:00 +0430 Date String.

How to convert concatenated time and date string into date?

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) {}
}

Convert a date and time string according to UTC format

I am having a date string 2012-11-21 13:11:25 which I get from local database. I have to convert this according to UTC settings and display it on a particular screen. So if its GMT+05:30 it should be displayed as 2012-11-21 18:41:25 on the screen. How can I do this conversion. I have checked some of the questions but that didn't work out.
I am able to get a Date object that returns something like Wed Nov 21 13:11:25 GMT+05:30 2012 after this I need to get the time as 18:41:25 and date as 11-21-2012
Thanks in advance
Your df and inputFmt must use the same format.
But I think you should do it like this:
Date myDate = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
calendar.setTime(myDate);
Date time = calendar.getTime();
SimpleDateFormat outputFmt = new SimpleDateFormat("MMM dd, yyy h:mm a zz");
String dateAsString = outputFmt.format(time);
System.out.println(dateAsString);
Get UTC from current time :
public String getCurrentUTC(){
Date time = Calendar.getInstance().getTime();
SimpleDateFormat outputFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
outputFmt.setTimeZone(TimeZone.getTimeZone("UTC"));
return outputFmt.format(time);
}
Best way to get formatted string of Date in required format is
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String formatted = dateFormat.format(date);
//This is my input date
String dtStart = "2019-04-24 01:22 PM";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm a");
Date date = null;
try {
date = format.parse(dtStart);
getDateInUTC(date)
} catch (ParseException e) {
e.printStackTrace();
}
//This Method use for convert Date into some UTC format
public static String getDateInUTC(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateAsString = sdf.format(date);
System.out.println("UTC" + dateAsString);
return dateAsString;
}

Convert Date time in "Mon APR 6, 2012 11:12 am" 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.

Categories

Resources