I have timestamp in DataBase like this: 1472373943.
In my Adapter I would like set Date & Time to different TextView.
For Example:
How to separate the date and time? Thanks.
First, what is your input? In your question, it looks like 1472373943, So I don't know which is time, and date position in that string. For simple, let assume that you have a sample date and time as follows:
String date = "12/8/2012";
String time = "11:25 am";
So, to convert it. The simple way is
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateTest {
public static void main(String[] args) {
String date = "12/8/2012";
String time = "11:25 am";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
try {
Date dt = df.parse(date + " " + time);
Calendar ca = Calendar.getInstance();
ca.setTime(dt);
System.out.println(ca.getTimeInMillis());
} catch (ParseException e) {
e.printStackTrace();
}
}
}
You can do that by using two different SimpleDateFormatters on the same Date object, one for the date and the other for the hours. "YYYY/MM/DD" and the other is "HH:mm a", just for an example.
Related
am c# developer and in c# you can use datetime.now to get this time as example but in android studio i do not know how to do that can you help me please .
not
I need just hour and minute thanks
You could use this example to get the current date:
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class GetCurrentDateTime {
public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//get current date time with Date()
Date date = new Date();
System.out.println(dateFormat.format(date));
//get current date time with Calendar()
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));
}
}
Using either Date or Calendar gives the same result in this case. But if you need to set particular dates or do date arithmetic, use a Calendar. Calendars also handle localisation.
In Android you are programming in Java. Java Date provides the functionality you are looking for.
https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
How to get current week of the year using date? Below code returns current week of the year using current time. How do I change this code to take input date like 26/8/2013?(in this caseit should return 35). I want to implement by taking input date form user then return current week of the year.
Calendar calender = Calendar.getInstance();
Log.d("Current Week:", "" + calender.get(Calendar.WEEK_OF_YEAR));
Toast.makeText(this, "current year weeks is"+ calender.get(Calendar.WEEK_OF_YEAR),
Toast.LENGTH_SHORT).show();
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import android.net.ParseException;
import android.widget.Toast;
...
/**
* #Param userInput Input date from user, format: "26/8/2013"
*/
public void getDayOfWeek(String userInput) {
Date date;
SimpleDateFormat format = new SimpleDateFormat("dd/M/yyyy");
try {
date = format.parse(userInput);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calender = new GregorianCalendar();
calender.setTime(date);
Toast.makeText(this, "current year weeks is" + calender.get(Calendar.WEEK_OF_YEAR), Toast.LENGTH_SHORT).show();
}
I have a textview which shows the day of the week as an integer (0-7). I would prefer if it could convert that to a string, which could then be shown in a TextView. My code is below. Also, how can I make it so the TextViews update the time, date, etc. (it only shows the time the app is opened)? Thanks in advance.
MainActivity.java:
package press.linx.calendar;
import java.sql.Date;
import java.text.SimpleDateFormat;
import android.os.Bundle;
import android.app.Activity;
import android.text.format.Time;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView day = (TextView)findViewById(R.id.day);
TextView month = (TextView)findViewById(R.id.month);
TextView year = (TextView)findViewById(R.id.year);
TextView time = (TextView)findViewById(R.id.time);
Time today = new Time(Time.getCurrentTimezone());
today.setToNow();
day.setText("" + today.monthDay); // Day of the month (0-31)
month.setText("" + today.month); // Month (0-11)
year.setText("" + today.year); // Year
time.setText("" + today.format("%k:%M")); // Current time
}
}
UPDATE: I got it using this piece of code:
final Calendar calendar = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("MMM"); // 3-letter month name & 2-char day of month
TextView datetxt = (TextView) findViewById(R.id.nameofyourtextview);
datetxt.setText(formatter.format(calendar.getTime()));
To format your Time:
Time time = new Time();
time.format("%A");
It returns name of day in week (Sunday, Friday..) - see description of format string (It's a PHP man page, but the symbols are same and it's well-aranged)
In order to make textViews updated every second you have to use Timer and TimerTask.
Define UpdateTimeTask:
class UpdateTimeTask extends TimerTask {
public void run() {
// Update time, must be called using runOnUiThread
}
}
and then set timer:
Timer timer = new Timer();
TimerTask updateTime = new UpdateTimeTask();
timer.scheduleAtFixedRate(updateTime, 0, 1000);
I assume you are looking for the date to be displayed in the below format.
You can use the below
Date now = new Date();
SimpleDateFormat dateFormatter = new SimpleDateFormat("EEEE, MMMM d, yyyy");
System.out.println("Format : " + dateFormatter.format(now));
Output
Format : Thursday, April 25, 2013
Few helpful links
http://www.ntu.edu.sg/home/ehchua/programming/java/DateTimeCalendar.html
http://www.roseindia.net/tutorial/java/core/convertDateToWords.html
To get the current day of the week (i.e. Monday, Tuesday, Wednesday, etc.) try:
DateFormat fmt = new SimpleDateFormat( "EEEE" );
fmt.format( new java.util.Date() );
Try this
month.setText(getMonth(today.month));
day.setText(getWeek(today.monthDay));
method to get month based on month number
public String getMonth(int month) {
return new DateFormatSymbols().getMonths()[month];
}
method to get week based on week number
public String getWeek(int weekno) {
return new DateFormatSymbols().getWeekdays()[weekno];
}
Do you mean display as Mon, Tue, Wed,.... ?
Use this format.
SimpleDateFormat curFormatDate = new SimpleDateFormat("EEE");
am working on countdown widget .The problem is explained below
'2012-07-04T15:00:00Z' - > '1341414000000'
'1341414000000' - > indicate 2012 july 4th 20:30
why this happend? . Am using joda
final String format = "yyyy-MM-dd'T'HH:mm:ssZ";
DateTimeFormatter formatter = DateTimeFormat.forPattern(format);
DateTime endTime = formatter.parseDateTime(strDate);
long diff=endTime.getMillis();
String time="2012-07-04T15:00:00Z";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
df.setTimeZone(TimeZone.getTimeZone("UTC"));
// time.replace("Z","");
try {
Date date=df.parse(time);
long diff=date.getTime()-System.currentTimeMillis();
System.out.println("Date "+diff);
} catch (ParseException e) {
e.printStackTrace();
}
This seems like an old question, but I'll answer it anyway, since other people may find this.
In Joda there is a class for ISO 8601 formats, so instead of specifying the format manually you could use that class, as follows:
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import org.joda.time.DateTime;
String strDate = "2012-07-04T15:00:00Z";
DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis();
DateTime endTime = formatter.parseDateTime(strDate);
long diff=endTime.getMillis();
On the other hand, the problem that you seem to be having is related to the time zone. When you convert back from millis to date string, it gets converted using the local time zone. If you want to get the date as UTC, you should do the following:
import org.joda.time.DateTimeZone;
import org.joda.time.DateTime;
DateTime dt = new DateTime(1341414000000).withZone(DateTimeZone.UTC);
It will return 2012-07-04T15:00:00.000Z as expected. If you want to format it without the milliseconds, you can use the same formatter as before:
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import org.joda.time.DateTime;
DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis();
DateTime dt = new DateTime(1341414000000).withZone(DateTimeZone.UTC);
formatter.print(dt)
And it will return 2012-07-04T15:00:00Z.
is it possible to convert Sms table date value e.g. 1293457709636 (miliseconds) to meaningful Date time value.
just do
long ms = 1293457709636; // or whatever you have read from sms
Date dateFromSms = new Date(ms);
You can convert Sms table date in to meaningful Date time like this.
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");
Calendar calendar = Calendar.getInstance();
long now = 1293457709636L;
calendar.setTimeInMillis(now);
Log.i("time", "time"+formatter.format(calendar.getTime()));