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

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));

Related

setText firebase TimeStamp to a TextView in Android

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.

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());

Java util.numberFormatException for input string: "2014-01-12 05-44-56"

I'm new in OFBiz, and Java. I used bellow block of code for checking date time input and use that for searching in table.
Timestamp strtDate = UtilDateTime.getTimestamp((String)request.getParameter("strt_date"));
if(strtDate != null)
{
// then here i used the date for taking data.
}
When i fill the date time field of form to search or when no date is selected for searching error occure that show numberFormatException, so how i can solve that? thanks for any help and guide.
Based on the Apache ofbiz API it looks like UtilDateTime#getTimestamp(String) expects milliseconds value. You are passing in "2014-01-12 05-44-56". You need to parse your date first. With pure pre 1.8 java (keep in mind that formatters aren't thread safe):
String dateString = "2014-01-12 05-44-56";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
Date date = formatter.parse(dateString);
UtilDateTime.getTimestamp(date.getTime());
Since java 1.8 (highly recommended to switch if you can!):
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH-mm-ss");
ZonedDateTime date = ZonedDateTime.parse(text, formatter);
long millis = date.toInstant().toEpochMilli();
you have to pass time in milliseconds not as you are passing.
you can check code also :
public static Timestamp getTimestamp(String milliSecs) throws NumberFormatException {
return new Timestamp(Long.parseLong(milliSecs));
}
it will parse the data in long which you are passing and that should be valid long value.
request.getParameter("strt_date") will anyways return String, so no need to cast it explicitly to String. Moreover, there will be a contract between Client & Server on the required Date Format. So you have to parse the String-Date in the same format using SimpleDateFormat. Code outlook will look like bellow:
SimpleDateFormat formatter = new SimpleDateFormat("contract-date-format");
Date date = formatter.parse(request.getParameter("strt_date"));
UtilDateTime.getTimestamp(date.getTime());

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());

How to display the date format in android?

String date="21-04-2013";
In my android application
Here i want to display the date in the following format like "21" is a separate string and month is like "Apr" as a separate string and year is like "13"as a separate string without using String functions.Can anybody plz give some suggestions to convert in this format?any date function is available?
You'll want to take a look at the SimpleDateFormat class for parsing the date string. In order to end up separate strings without using string functions, you'll probably need multiple formatters for the output too. It would look somewhat like this:
String date = "21-04-2013";
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MM-yyyy"); // input date
Date outDate = dateFormatter.parse(date);
SimpleDateFormat dayFormatter = new SimpleDateFormat("dd"); // output day
SimpleDateFormat monthFormatter = new SimpleDateFormat("MMM"); // output month
SimpleDateFormat yearFormatter = new SimpleDateFormat("yy"); // output year
String day = dayFormatter.format(outDate);
String monthy = monthFormatter.format(outDate);
String year = yearFormatter.format(outDate);
If you were to use String.split(), you could get rid of at least two of the formatters in above snippet.
This is the class you'll need: DateFormat
Examples are provided in the link, but in short, you'll first need to parse the date, and then format the date again.

Categories

Resources