-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathApp.java
More file actions
57 lines (52 loc) · 1.53 KB
/
App.java
File metadata and controls
57 lines (52 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.github.archarithms;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
public class App
{
public static String convertToTitleCase(final String inpStr)
{
{
//check for null
if(inpStr == null) {
throw new NullPointerException();
}
//make string lower case
String input = inpStr.toLowerCase();
//remove all non alpha numeric
input = input.replaceAll("[^a-zA-Z0-9]"," ");
//remove excess whitespace
input = input.replaceAll("\\s+"," ");
//Captialize Words
char[] charArray = input.toCharArray();
//set foundspace true so first letter is always capatilized
boolean foundSpace = true;
for(int i=0; i < charArray.length; i++)
{
if(Character.isLetter(charArray[i]) && foundSpace)
{
charArray[i] = Character.toUpperCase(charArray[i]);
foundSpace = false;
}
if(charArray[i]==' ')
{
foundSpace=true;
}
}
//return completed string
return String.valueOf(charArray);
}
}
public static String convertUnixToDateString(final Long inpUnixSeconds)
{
//check for null
if(inpUnixSeconds==null)
{
throw new NullPointerException();
}
//convert from unix epoch time to human readable time
else return LocalDateTime.ofInstant(Instant.ofEpochSecond(inpUnixSeconds),
ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("MMMM d, YYYY"));
}
}