setText firebase TimeStamp to a TextView in Android - java

I'm reading data from cloud firestore one of which is a TimeStamp. So how I can set that TimeStamp to a TextView in an activity in SimpleDateFormat.

You can first turn the timeStamp to actual date format using a code like this:
private String getDate(long time) {
Calendar cal = Calendar.getInstance(Locale.ENGLISH);
cal.setTimeInMillis(time);
String date = DateFormat.format("dd-MM-yyyy", cal).toString();
return date;
}
Now you can set this date to textView using setText() method, using a code like below:
textView.setText(getDate(timestamp));
Here timeStamp can be the time stamp that you retrieve from your Firebase cloud Firestore.

Related

How can I read date ( timestamp ) from Cloud-Firestore?

I have a ProfileFragment where I want to show the user's profile. I also added the birth date in Firestore and now I want to display it for the current user on his profile so, how can I do that? Do I have to convert it to a string or what should I do ? I tried to make it a string with .toString() but it isn't displayed.If I let it like that or make it a Date it will show me an error when I try to "setText" to the TextView variable.
TVbirthdate = getActivity().findViewById(R.id.birthdateinfo);
Timestamp birthResult = task.getResult().getTimestamp("Birth Date");
TVbirthdate.setText(birthResult.toDate());
Thanks!
You have to convert the timestamp to the actual date format first before using it on the setText() method. See sample converter function below:
private String getDate(long time) {
Calendar cal = Calendar.getInstance(Locale.ENGLISH);
cal.setTimeInMillis(time);
String date = DateFormat.format("dd-MM-yyyy", cal).toString();
return date;
}
After converting, you can now set this date using the setText() method. See sample code below:
// birthResult here is the Timestamp object from Firestore
TVbirthdate.setText(getDate(birthResult));
If the above code is not possible for your use-case, you can also use SimpleDateFormat. See sample implementation below:
Date dataDate = birthResult.toDate();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
TVbirthdate.setText(sdf.format(dataDate));

Time stamp is not being written to Firebase database

I want to write a time stamp to the Firebase database so that I can later set what items can and can not be read from the Firebase database according to the time set. In my application I have a timpicker which the user uses to set the time, after which the set time is attached to the current date as shown below
Calendar c=Calendar.getInstance();
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
String dateToday=df.format(c.getTime());
dateandtime=dateToday+time;
After which I convert dateandtime into a timestamp using the method below
try {
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
Date parsedDate=dateFormat.parse(dateandtime);
timestamp=new Timestamp(parsedDate.getTime());
}catch (Exception e){}
My class called Travel details for writing to the database is like this
......
private Timestamp pickuptime;
public Timestamp getPickuptime(){
return pickuptime;}
public void setPickuptime(Timestamp pickuptime){
this.pickuptime=pickuptime;
}
And this is how I write the time to the database in my main activity
Timestamp timestamp;
.............
d.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TravelDetails travelDetails=new TravelDetails() ;
travelDetails.setPickuptime(timestamp);
myRef.child(userID).push().setValue(
travelDetails);
Ali.push().setValue(travelDetails);
Instead of writing the time stamp to the database everything else from travel details class is written to the Firebase database except the time stamp. Kindly render assistance.
Firebase Realtime Database doesn't support writing Timestamp type values. It looks like you might be trying to use a Cloud Firestore Timestamp with Realtime Database. It doesn't work that way - they are different database systems. Timestamp objects can only be used with Cloud Firestore documents.
If you want to record a point in time with Realtime Database, you typically just use a number value for that.

Adding 30 days on the value from JDateChooser using Java netbeans

hi all I am working on a form using JDateChooser I want to get the value of date inputted by the user. Is there any ways that after storing the value of date in a variable can I add 30 days from the inputted date? This is my code in passing the date to a string variable:
String dates =((JTextField)date.getDateEditor().getUiComponent()).getText();
my problem is how can I pass the date into a variable where i can be able to add an additional 30 days on it?
please help me really need this for my project .
To manipulate a String with a date value, you first need to convert to a Date using SimpleDateFormat, then you can perform manipulation using a Calendar.
SimpleDateFormat datefmt = new SimpleDateFormat("MM/dd/yyyy"); // Or format you're using
Date date = datefmt.parse(dates);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, 30); // Add 30 days
Date futureDate = cal.getTime();
If you need value for insertion into a database, you will of course use PreparedStatement, so you'll need a Timestamp instead:
// From Date
Timestamp futureTimestamp = new Timestamp(futureDate.getTime());
// Directly from Calendar
Timestamp futureTimestamp = new Timestamp(cal.getTimeInMillis());

Parse JSON in UNIX date in Java/Android without any library

I have a JSON array received from my server. I obtain a date as a timestamp String – for example, [{"date": "1418056895", ... }] – and I need to parse it in our day format. How do I do this? I have tried but I always have the year as 1970.
Note: My JSON format is in UNIX.
JSONObject feedObj = (JSONObject) feedArray.get(i);
FeedItem item = new FeedItem();
item.setTimeStamp(feedObj.getString("date"));
public void setTimeStamp(String timeStamp) {
this.timeStamp = timeStamp;
}
public String getTimeStamp() {
return timeStamp;
}
In another class, I parse the timestamp to display it:
List<FeedItem> feedItems = ...;
FeedItem item = feedItems.get(position);
CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
Long.parseLong(item.getTimeStamp()),
System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);
However the displayed date is always in 1970.
Using values such as 1403375851930 it appears to work as expected (it displays a contemporary date).
If you are parsing JSON correctly,
feedObj.getString("date");
holds your date. You are reading date as String. You have to convert it to long.
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
Long timestamp = Long.parseLong("1418056895"); //Long.parseLong(feedObj.getString("date"));
cal.setTimeInMillis(timestamp * 1000L);
DateFormat format = new SimpleDateFormat("MM/dd/yyyy"); // what ever format you need.
System.out.println(format.format(cal.getTime())); // this will be in MM/dd/yyyy
//item.setTimeStamp(format.format(cal.getTime()));
}
you can also use feedObj.getLong("date"); to get Date in long format directly, but it depends on which JSON library you using.
I don't really understand what you are trying to ask here. But if you're asking is to ow to convert that date that you get in human readable form then you should take a look at Epoch Converter. The date is returned to you in epoch timestamp. You can convert the same to a human readable form n java by using the following code.
String date = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new java.util.Date (epoch*1000));
I have checked your api documentation and the date you have in response is in unix time format as described here wall.post
You can convert it using the method described here Convert Unix time stamp to date in java
Once converted you can format the date as described below.
private List<FeedItem> feedItems;
TextView timestamp
FeedItem item = feedItems.get(position);
Long timeObject = Long.parseLong(getTimeStamp());
//use the timeObject and convert it in the String as described
// in above link to convert timestamp in date object than in String
//Once available set it again in timestamp
timestamp.setText(formattedDate);

How to retrieve chosen date from JDatechooser?

I am using Eclipse to do a college project on Java. I realized that java does not have a built in date selector like C#, so I downloaded and added JDateChooser. I tried to retrieve the chosen date but it failed:
String Date = dateChooser.getDate(); //I want to the date to be retrieved as string
Any ideas? Is there some kind of initialization that I must do?
Retrieve the date that the user selected by calling getDate(), which returns a Date object. Then convert that object into a String by calling SimpleDateFormat.format():
Date d;
SimpleDateFormat sdf;
String s;
d = dateChooser.getDate(); // Date selected by user
sdf = SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); // Or whatever format you need
s = sdf.format(d); // Viola
See also: Question 5683728
If I have the right component, the Java Docs show that the getDate method returns Date
getDate
public java.util.Date getDate() Returns the date. If the
JDateChooser is started with a null date and no date was set by the
user, null is returned. Returns: the current date
String dob =new SimpleDateFormat("dd-MMM-yyyy").format(jDateChooser1.getDate());

Categories

Resources