so i am making a messaging app but i cannot get the code done right
this is the send Button:
sendbutton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View _view) { if (img == 0) { if (edittext1.getText().toString().equals("")) { } else { Numberrr++; mm = new HashMap<>(); mm.put("username", get_name); mm.put("pfp", get_pfp); mm.put("messages", edittext1.getText().toString().trim()); mm.put("user_uid", FirebaseAuth.getInstance().getCurrentUser().getUid()); mm.put("number", String.valueOf((long)(Numberrr))); cal = Calendar.getInstance(); mm.put("time", new SimpleDateFormat("E hh:mm s").format(cal.getTime())); ch.push().updateChildren(mm); mm.clear(); edittext1.setText(""); lastText.edit().putString("Lasttext", String.valueOf((long)(Numberrr))).commit(); } } else { store.child(imgName).putFile(Uri.fromFile(new File(imgPath))).addOnFailureListener(_store_failure_listener).addOnProgressListener(_store_upload_progress_listener).continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() { @Override public Task<Uri> then(Task<UploadTask.TaskSnapshot> task) throws Exception { return store.child(imgName).getDownloadUrl(); } }).addOnCompleteListener(_store_upload_success_listener); progressbar1.setVisibility(View.VISIBLE); sendbutton.setEnabled(false); edittext1.setEnabled(false); } } });
and this is part of the code for the ListView:
LayoutInflater _inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View _view = _v; if (_view == null) { _view = _inflater.inflate(R.layout.messgebubble, null); } final LinearLayout time = _view.findViewById(R.id.time); final TextView textview1 = _view.findViewById(R.id.textview1); final LinearLayout linear4 = _view.findViewById(R.id.linear4); final TextView timetext = _view.findViewById(R.id.timetext); final LinearLayout left = _view.findViewById(R.id.left); final LinearLayout linear1 = _view.findViewById(R.id.linear1); final de.hdodenhof.circleimageview.CircleImageView circleimageview1 = _view.findViewById(R.id.circleimageview1); final TextView message = _view.findViewById(R.id.message); final ImageView imageview1 = _view.findViewById(R.id.imageview1); final TextView SystemText = _view.findViewById(R.id.systemtext); final LinearLayout system = _view.findViewById(R.id.system); //This is to set the time on text view timetext.setText(new Sim("MM/yyyy hh:mm:ss").format(_data.get((int)_position).get("time").toString())); //end
so im trying to get messages to display the right time for messages while in different timezone
e.g. if someone is in (UTC+1) but the message is from (UTC+5)and the message was sent at 17:35(UTC+5) it would show 17:35 but it should be showing 14:35… as you can tell this will cause a lot of problems in code also.
edit: a revision
Advertisement
Answer
Your question is not clear. But likely these points will help you:
- Never use the legacy date-time classes such as
Date
,Calendar
,SimpleDateFormat
, etc. They are terrible. Use only the modern java.time classes defined in JSR 310. - To track a moment, a specific point on the timeline, do so “in UTC” (an offset of zero hours-minutes-seconds from UTC), using the
java.time.Instant
class. - To serialize a moment textually for data storage or data exchange, use the standard ISO 8601 format. Usually best to do so in UTC, using
Instant#toString
andInstant.parse
.
Here is some brief code. But search to learn more. All this has been covered many many times already on Stack Overflow.
Capture current moment
Use java.time.Instant
to represent a moment as seen in UTC.
Instant instant = Instant.now() ; // Capture the current moment as seen in UTC. String s = instant.toString() ; // Generate text in standard ISO 8601 format.
Parse
Here we parse text in standard ISO 8601 format.
Instant instant = Instant.parse( "2022-04-19T15:30:14.348767Z" ) ;
Adjust into time zone
For presentation to the user, you may want to adjust to a particular time zone. Same moment, same point on the timeline, different wall-clock time.
ZoneId z = ZoneId.of( "America/Edmonton" ) ; ZonedDateTime zdt = instant.atZone( z ) ;
Generate text
Standard format
To generate text in standard format, simply call toString
.
instant.toString(): 2022-04-19T15:30:14.348767Z
zdt.toString(): 2022-04-19T09:30:14.348767-06:00[America/Edmonton]
Actually that last one is an wise extension of the standard format, appending the name of the time zone in square brackets.
Localized format
To generate text in a localized format, use DateTimeFormatter.ofLocalizedDateTime
.
FormatStyle style = FormatStyle.SHORT ; Locale locale = Locale.CANADA_FRENCH ; // Or Locale.US or new Locale( "fr" , "MA" ) (French in Morocco), etc. DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( style ).withLocale( locale ) ; String output = zdt.format( f );
See this code run live at IdeOne.com.
output: 22-04-19 09 h 30
You can of course specify a custom format via DateTimeFormatter.ofPattern
. (Search to learn more.) But automatically localizing might make your life simpler.