-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
43 changed files
with
2,552 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.taopao.tpmusicdemo; | ||
|
||
import org.apache.commons.lang3.time.DurationFormatUtils; | ||
|
||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Calendar; | ||
import java.util.Date; | ||
|
||
/** | ||
* @Author: TaoPao | ||
* @Date: 3/26/21 10:42 AM | ||
* @Description: java类作用描述 | ||
*/ | ||
public class Test { | ||
|
||
public static void main(String args[]) { | ||
|
||
System.out.print("年龄:"+TpDateUtils.getAge("2019-8-30")); | ||
System.out.print("\n年"+TpDateUtils.getAge2Year("2019-8-30")); | ||
System.out.print("\n月"+TpDateUtils.getAge2Monh("2019-8-30")); | ||
System.out.print("\n日"+TpDateUtils.getAge2Day("2019-8-30")); | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
165 changes: 165 additions & 0 deletions
165
app/src/main/java/com/taopao/tpmusicdemo/TpDateUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
package com.taopao.tpmusicdemo; | ||
|
||
import org.apache.commons.lang3.time.DurationFormatUtils; | ||
|
||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Calendar; | ||
import java.util.Date; | ||
|
||
/** | ||
* @Author: TaoPao | ||
* @Date: 3/26/21 4:29 PM | ||
* @Description: java类作用描述 | ||
*/ | ||
public class TpDateUtils { | ||
|
||
public static int getYear(String startTime, String endTime) { | ||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | ||
try { | ||
Date startDate = sdf.parse(startTime); | ||
Date endDate = sdf.parse(endTime); | ||
return getBaseDateType(startDate, endDate, "yyyy"); | ||
} catch (ParseException e) { | ||
e.printStackTrace(); | ||
} | ||
return 0; | ||
} | ||
|
||
public static int getYear(Date startDate, Date endDate) { | ||
return getBaseDateType(startDate, endDate, "yyyy"); | ||
} | ||
|
||
|
||
public static int getMonth(String startTime, String endTime) { | ||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | ||
try { | ||
Date startDate = sdf.parse(startTime); | ||
Date endDate = sdf.parse(endTime); | ||
return getBaseDateType(startDate, endDate, "MM"); | ||
} catch (ParseException e) { | ||
e.printStackTrace(); | ||
} | ||
return 0; | ||
} | ||
|
||
public static int getMonth(Date startDate, Date endDate) { | ||
return getBaseDateType(startDate, endDate, "MM"); | ||
} | ||
|
||
|
||
public static int getDay(String startTime, String endTime) { | ||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | ||
try { | ||
Date startDate = sdf.parse(startTime); | ||
Date endDate = sdf.parse(endTime); | ||
return getBaseDateType(startDate, endDate, "dd"); | ||
} catch (ParseException e) { | ||
e.printStackTrace(); | ||
} | ||
return 0; | ||
} | ||
|
||
public static int getDay(Date startDate, Date endDate) { | ||
return getBaseDateType(startDate, endDate, "dd"); | ||
} | ||
|
||
private static int getBaseDateType(Date startDate, Date endDate, String format) { | ||
String nowDateString = ""; | ||
if (endDate.getTime() >= startDate.getTime()) { | ||
nowDateString = DurationFormatUtils.formatPeriod( | ||
startDate.getTime(), | ||
endDate.getTime(), | ||
format); | ||
} else { | ||
nowDateString = DurationFormatUtils.formatPeriod( | ||
endDate.getTime(), | ||
startDate.getTime(), | ||
format); | ||
} | ||
return Integer.parseInt(nowDateString); | ||
} | ||
|
||
public static int getAge2Day(String startTime) { | ||
Date date = new Date(); | ||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | ||
String format = sdf.format(date); | ||
return getDay(startTime, format); | ||
} | ||
|
||
public static int getAge2Monh(String startTime) { | ||
Date date = new Date(); | ||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | ||
String format = sdf.format(date); | ||
return getMonth(startTime, format); | ||
} | ||
|
||
public static int getAge2Year(String startTime) { | ||
Date date = new Date(); | ||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | ||
String format = sdf.format(date); | ||
return getYear(startTime, format); | ||
} | ||
|
||
public static String getAge(String startTime) { | ||
Date date = new Date(); | ||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | ||
String format = sdf.format(date); | ||
return getAge(startTime, format); | ||
} | ||
|
||
public static String getAge(String startTime, String endTime) { | ||
try { | ||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | ||
Date startDate = sdf.parse(startTime); | ||
Date endDate = sdf.parse(endTime); | ||
|
||
String nowDateString = ""; | ||
if (endDate.getTime() >= startDate.getTime()) { | ||
nowDateString = DurationFormatUtils.formatPeriod( | ||
startDate.getTime(), | ||
endDate.getTime(), | ||
"yyyy-MM-dd"); | ||
} else { | ||
nowDateString = DurationFormatUtils.formatPeriod( | ||
endDate.getTime(), | ||
startDate.getTime(), | ||
"yyyy-MM-dd"); | ||
} | ||
|
||
Date nowDate = sdf.parse(nowDateString); | ||
Calendar nowCalendar = Calendar.getInstance(); | ||
nowCalendar.setTime(nowDate); | ||
|
||
int nowYear = nowCalendar.get(Calendar.YEAR); //当前年份 | ||
int nowMonth = nowCalendar.get(Calendar.MONTH) + 1; //当前月份 | ||
int nowDay = nowCalendar.get(Calendar.DAY_OF_MONTH); //当前日期 | ||
|
||
|
||
String age = ""; | ||
if (nowYear > 0) { | ||
age = nowYear + "年"; | ||
} | ||
if (nowMonth > 0) { | ||
age = age + nowMonth + "月"; | ||
} else { | ||
if (nowYear > 0) { | ||
age = age + "零"; | ||
} | ||
} | ||
if (nowDay >= 0) { | ||
age = age + nowDay + "天"; | ||
} | ||
if (endDate.getTime() >= startDate.getTime()) { | ||
return age; | ||
} else { | ||
return "距离出生还有" + age; | ||
} | ||
|
||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return ""; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:orientation="vertical" | ||
tools:context=".MainActivity"> | ||
|
||
<TextView | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="Hello World!" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintLeft_toLeftOf="parent" | ||
app:layout_constraintRight_toRightOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> | ||
<Button | ||
android:id="@+id/btn_start" | ||
android:text="开启服务" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content"> | ||
</Button> | ||
|
||
|
||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
plugins { | ||
id 'com.android.library' | ||
} | ||
|
||
android { | ||
compileSdkVersion 30 | ||
buildToolsVersion "30.0.2" | ||
|
||
defaultConfig { | ||
minSdkVersion 19 | ||
targetSdkVersion 30 | ||
versionCode 1 | ||
versionName "1.0" | ||
|
||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
consumerProguardFiles "consumer-rules.pro" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
} | ||
|
||
dependencies { | ||
|
||
implementation 'androidx.appcompat:appcompat:1.1.0' | ||
implementation 'com.google.android.material:material:1.1.0' | ||
testImplementation 'junit:junit:4.+' | ||
androidTestImplementation 'androidx.test.ext:junit:1.1.1' | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' | ||
} |
Empty file.
Oops, something went wrong.