Skip to content

Commit

Permalink
Use Java 8 Date/Time API instead of java.util.Date.
Browse files Browse the repository at this point in the history
  • Loading branch information
Isira-Seneviratne committed Jul 19, 2020
1 parent 82c96ff commit a1c07a9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,7 @@ public void handleResult(@NonNull final StreamInfo info) {

if (info.getUploadDate() != null) {
videoUploadDateView.setText(Localization
.localizeUploadDate(activity, info.getUploadDate().date().getTime()));
.localizeUploadDate(activity, info.getUploadDate().date()));
videoUploadDateView.setVisibility(View.VISIBLE);
} else {
videoUploadDateView.setText(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import com.xwray.groupie.kotlinandroidextensions.GroupieViewHolder
import icepick.State
import io.reactivex.disposables.CompositeDisposable
import java.io.File
import java.text.SimpleDateFormat
import java.util.Date
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.Locale
import kotlin.math.floor
import kotlin.math.max
Expand Down Expand Up @@ -189,7 +189,8 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
}

private fun onExportSelected() {
val date = SimpleDateFormat("yyyyMMddHHmm", Locale.ENGLISH).format(Date())
val date = DateTimeFormatter.ofPattern("yyyyMMddHHmm", Locale.ENGLISH)
.format(LocalDateTime.now())
val exportName = "newpipe_subscriptions_$date.json"
val exportFile = File(Environment.getExternalStorageDirectory(), exportName)

Expand Down
12 changes: 5 additions & 7 deletions app/src/main/java/org/schabi/newpipe/report/ErrorActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.Vector;

import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
Expand Down Expand Up @@ -229,7 +229,6 @@ protected void onCreate(final Bundle savedInstanceState) {
openPrivacyPolicyDialog(this, "GITHUB");
});


// normal bugreport
buildInfo(errorInfo);
if (errorInfo.message != 0) {
Expand Down Expand Up @@ -491,9 +490,8 @@ public void onBackPressed() {
}

public String getCurrentTimeStamp() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
return df.format(new Date());
return DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm").withZone(ZoneId.of("GMT"))
.format(ZonedDateTime.now());
}

public static class ErrorInfo implements Parcelable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.Map;
import java.util.zip.ZipFile;
Expand Down Expand Up @@ -177,8 +177,8 @@ public void onActivityResult(final int requestCode, final int resultCode,
&& resultCode == Activity.RESULT_OK && data.getData() != null) {
String path = Utils.getFileForUri(data.getData()).getAbsolutePath();
if (requestCode == REQUEST_EXPORT_PATH) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);
exportDatabase(path + "/NewPipeData-" + sdf.format(new Date()) + ".zip");
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss", Locale.US);
exportDatabase(path + "/NewPipeData-" + dtf.format(LocalDateTime.now()) + ".zip");
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.override_current_data)
Expand Down
18 changes: 12 additions & 6 deletions app/src/main/java/org/schabi/newpipe/util/Localization.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

Expand Down Expand Up @@ -137,13 +140,16 @@ public static String localizeNumber(final Context context, final double number)
return nf.format(number);
}

public static String formatDate(final Date date, final Context context) {
return DateFormat.getDateInstance(DateFormat.MEDIUM, getAppLocale(context)).format(date);
public static String formatDate(final LocalDate date) {
return DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).format(date);
}

@SuppressLint("StringFormatInvalid")
public static String localizeUploadDate(final Context context, final Date date) {
return context.getString(R.string.upload_date_text, formatDate(date, context));
public static String localizeUploadDate(final Context context, final Calendar calendar) {
final ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(calendar.toInstant(),
ZoneId.systemDefault());
return context.getString(R.string.upload_date_text, formatDate(zonedDateTime
.toLocalDate()));
}

public static String localizeViewCount(final Context context, final long viewCount) {
Expand Down

0 comments on commit a1c07a9

Please sign in to comment.